Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

endthreadex Access Violation

bamram
Beginner
705 Views
Hi Everyone - I've written some multithread code which is receiving an Unhandled Exception 0xc000005:Access Violation the second time I attempt to execute a call to endthreadex in debug mode. These routines are part of a DLL and compiled/linked in DVF 6.6. Hopefully someone here can provide a clue. Thanks!
Here's a snippet of the routine which kicks off the new thread:
Subroutine mexFunction(nlhs, plhs, nrhs, prhs)
USE KERNEL32
implicit none
!DEC$ ATTRIBUTES STDCALL, ALIAS : '_OSCDRV' :: OSCDRV
interface
function beginthreadex (security, stack_size, start_address,
&arglist,initflag, thrdaddr)
use dfwinty
integer(UINT) :: beginthreadex
!DEC$ ATTRIBUTES C, alias:"__beginthreadex" :: beginthreadex
type(T_SECURITY_DESCRIPTOR), intent(IN) :: security
!DEC$ ATTRIBUTES REFERENCE, ALLOW_NULL :: security
integer(UINT), intent(IN) :: stack_size
integer(PVOID), intent(IN) :: start_address
integer(PVOID), intent(IN) :: arglist
integer(UINT), intent(IN) :: initflag
integer(UINT), intent(OUT):: thrdaddr
!DEC$ ATTRIBUTES REFERENCE,IGNORE_LOC :: thrdaddr
end function beginthreadex
end interface
integer plhs(*), prhs(*)
integer nlhs, nrhs
integer*4 ithread_id
integer*4 iWaitResult
integer*4 Hthread, thrdaddr
do i=1,nloop
- Do Something Here -
Hthread = beginthreadex(NULL,0,LOC(OSCDRV),0,CREATE_SUSPENDED,thrdaddr)
CSet OSC Thread Priority High to Increase Execution Speed
CAnd Start OSC Thread Execution
if (Hthread .NE. 0) then
ireturn = SetThreadPriority (Hthread,THREAD_PRIORITY_ABOVE_NORMAL)
ireturn = ResumeThread(Hthread)
else
iWaitResult = -9999
ErrorMsg = "OSC CreateThread Failed In Module mexFunction"
end if
CWait for OSC thread to finish
if (Hthread .NE. 0) then
iWaitResult = WaitForSingleObject (Hthread,INFINITE)
ireturn = CloseHandle( Hthread )
endif
enddo
C
return
end
Here's a snippet from the code executed in the new thread:
INTEGER(4) FUNCTION OSCDRV
USE KERNEL32
C
IMPLICIT DOUBLE PRECISION (A-H,O-Z)
C
interface
subroutine endthreadex (retval)
use dfwinty
!DEC$ ATTRIBUTES C, alias:"__endthreadex" :: endthreadex
integer(UINT), intent(IN) :: retval
end subroutine endthreadex
end interface
- Do something here
OSCDRV = 0
Call endthreadex(0)
END FUNCTION

Message Edited by BamRam on 07-11-2005 03:34 PM

0 Kudos
2 Replies
Jugoslav_Dujic
Valued Contributor II
705 Views
Sanity check: are both the exe file and the dll linked against the same set of both Fortran and C run-time libraries (multithreaded Dll, preferrably?).

If that's OK, you're pretty much on your own. You're on a quite moot territory of interaction between Fortran RTL, C RTL and multiple threads, which was unfortunately never (AFAICT) documented well in CVF docs or elsewhere. I do know that C code requires _beginthreadex and _endthreadex to avoid memory leaks in C RTL, but it was never explicitly stated for Fortran; all CVF samples use plain CreateThread.

As for myself, I routinely use CreateThread and always exit the threads by plain RETURN; I think that the potential memory leaks are too tiny to cause any problems for 99.99% of applications.

Jugoslav
0 Kudos
bamram
Beginner
705 Views
Thanks for your response Jugoslav!
Yes both the FORTRAN and C run-time libraries are linked multithreaded DLL.
This same problem occured when I used the CreateThread/ExitThread construct.
I don't want to use RETURN since this is prototype code for solving the problem with using STOP buried deep within the calling structure. The whole idea is to use endthreadex or ExitThread to break out of any location within the FORTRAN code and back to the code which kicked off the thread.
I've considered using divide by zero to create an exception and then writing C code to handle the exception. But I already have MATLAB calling a FORTRAN DLL and don't want to further confuse the situation by putting a C wrapper around the FORTRAN.
0 Kudos
Reply