Reputation:
I am a novice in the windows API, and recently I learned I should not use CreateThread and TerminateThread. I switched to _beginthreadex
, however I am not sure how I am supposed to use _endthreadex
?
For example, here is a basic function I was testing with:
// MyThreadFunction - outputs "#.) I Work" and ends
unsigned int __stdcall MyThreadFunction( void * lpParam )
{
int i = (int)lpParam;
cout << i << ".) I work! " << endl;
_endthreadex(0);
return 0;
}
Is my placement of _endthreadex
correct? I have it before the return 0, which just seems odd to me? I read in the msdn pages about _endthreadex
that it is called automatically when a function ends, but you should call it for better memory cleanup, and that is why I tried putting it in. It just doesn't seem right, sorry if this is a bad question. I just want to make sure I am doing everything right to the best of my abilities
Upvotes: 10
Views: 5338
Reputation: 73379
You actually don't need to call _endthreadex()
at all. It will be called for you automatically after your thread function returns anyway.
Upvotes: 10
Reputation: 69734
The answer is already given in short version, and if you fancy to know more - the return from your function will actually be used as an argument in _endthreadex
call. Here is what runtime is doing when you start a thread. The thread is indeed started with a starting point somewhere in CRT, where internal _callthreadstartex
is doing:
_endthreadex(MyThreadFunction(...));
That is, as soon as you return, _endthreadex
will be immediately called for you and it will exit from the thread.
Can you use it explicitly? Yes you can, and your code is also good because it does not matter much whether you call it yourself or it will be called for you, provided that no leaks take place on the way (such as, in particular, not yet called destructors of local variables).
Since it will anyway be called for you, and return from function is safer in terms of freeing local resources, there is no use, advantage and sense to make an explicit call.
MSDN basically explains exactly the same right there in Remarks section.
You can call _endthread or _endthreadex explicitly to terminate a thread; however, _endthread or _endthreadex is called automatically when the thread returns from the routine passed as a parameter to _beginthread or _beginthreadex. Terminating a thread with a call to endthread or _endthreadex helps ensure proper recovery of resources allocated for the thread.
Upvotes: 4
Reputation: 17444
You don't have to call it, as already mentioned. Concerning your code, I think that the final return 0;
will not even be called because _endthreadex()
never returns. This could also cause destructors of local objects not to be called (which is what I believe the MSDN wanted to say), so using this function could even be harmful in C++. Consider using Boost.Thread, they have an exception dedicated specifically to ending a thread, which should do the right thing if you want to terminate the thread somewhere in the middle.
Upvotes: 3