Where I use _endthreadex - windows API

I am new to the Windows API and I recently learned that I should not use CreateThread and TerminateThread. I switched to _beginthreadex , but I'm not sure how I should use _endthreadex ?

For example , here is the main function I tested:

 // 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 _endthreadex placement _endthreadex ? I have this before returning 0, which seems strange to me? I read on the msdn pages about _endthreadex that it is called automatically when the function ends, but you should call it to better clear the memory, and that is why I tried to insert it. It just doesn't seem right, sorry if this is a bad question. I just want to make sure that I am doing everything right as far as possible.

+8
c ++ c multithreading windows
source share
3 answers

You do not need to call _endthreadex() at all. It will be called for you automatically after the stream function returns anyway.

+9
source share

The answer is already indicated in the short version, and if you are interested in learning more, returning from your function will actually be used as an argument in the _endthreadex call. Here's what happens at runtime when starting a thread. The thread really starts from the starting point somewhere in the CRT, where the internal _callthreadstartex :

 _endthreadex(MyThreadFunction(...)); 

That is, as soon as you return, _endthreadex will be immediately called for you, and it will exit the stream.

Can you use it explicitly? Yes, you can, and your code is also good, because it does not matter whether you call it yourself or it will be called for you, provided that there are no leaks on the way (for example, in particular, local variables not yet named by destructors).

Since it will be called for you in any case, and returning from the function is safer from the point of view of freeing local resources, there is no benefit, advantage and sense for an explicit call.

MSDN basically explains exactly the same in the Remarks section.

You can explicitly call _endthread or _endthreadex to complete the thread; however, _endthread or _endthreadex is called automatically when the thread returns from the procedure passed as a parameter to _beginthread or _beginthreadex. Terminating a thread with an endthread or _endthreadex call helps ensure proper recovery of resources allocated to the thread.

+3
source share

You do not need to call it, as already mentioned. As for your code, I think the final return 0; will not even be called because _endthreadex() never returns. It can also cause the destructors of local objects to not be called (which, I believe, wanted to say MSDN), so using this function can even be harmful in C ++. Consider using Boost.Thread, they have an exception specifically designed to terminate the thread, which should do the right thing if you want to terminate the thread somewhere in between.

+2
source share

All Articles