I have often seen the thread functions written like the one below:
DWORD WINAPI MyThreadProc( LPVOID lpParam )
{
DoSomeGreatOperationsHere();
_endthreadex( 0 ); return 0; }
Do we really require to call
_endthreadex()
in the threads created using
_beginthreadex()
? A big NO is the answer!! and for that matter you don't need a call to
ExitThread()
too.
_beginthreadex()
function actually allocates and initializes the per thread data required by the CRT and calls
CreateThread()
passing a stub function in CRT as the entrypoint for the thread. This stub function will call your actual thread function(in this case,
MyThreadProc
) and when your thread function returns, the stub function ensures that it calls
_endthreadex()
passing the value returned from the thread function.
Again in
_endthreadex()
, the per thread data allocated will be released and
ExitThread()
will be called passing the value received in
_endthreadex()
.
Reference: threadex.c & thread.c in CRT Source.
Please be free to ask your queries, if any. :)