How to get a thread's exit code
-
Hi guys, I'm creating a multi-threaded application using
_beginthreadex()
and_endthreadex()
. If my thread executes correctly I call_endthreadex( 0 )
but if it doesn't I call_endthreadex( _<some error code>_ )
. How can I capture the thread's exit code in the main thread? Thanks. -
Hi guys, I'm creating a multi-threaded application using
_beginthreadex()
and_endthreadex()
. If my thread executes correctly I call_endthreadex( 0 )
but if it doesn't I call_endthreadex( _<some error code>_ )
. How can I capture the thread's exit code in the main thread? Thanks.«_Superman_» I love work. It gives me something to do between weekends.
Microsoft MVP (Visual C++) -
Hi guys, I'm creating a multi-threaded application using
_beginthreadex()
and_endthreadex()
. If my thread executes correctly I call_endthreadex( 0 )
but if it doesn't I call_endthreadex( _<some error code>_ )
. How can I capture the thread's exit code in the main thread? Thanks.masnu wrote:
If my thread executes correctly I call _endthreadex( 0 )
Just
return
ing would be the best way to exit a thread.“Follow your bliss.” – Joseph Campbell
-
masnu wrote:
If my thread executes correctly I call _endthreadex( 0 )
Just
return
ing would be the best way to exit a thread.“Follow your bliss.” – Joseph Campbell
Hi Rajesh, I know that returning from a thread will call
_endthreadex()
but everything I have read says that "terminating a thread with a call to endthread or _endthreadex helps to ensure proper recovery of resources allocated for the thread". Is it safe to assume that returning will accomplish this? Thanks, Paul -
Hi Rajesh, I know that returning from a thread will call
_endthreadex()
but everything I have read says that "terminating a thread with a call to endthread or _endthreadex helps to ensure proper recovery of resources allocated for the thread". Is it safe to assume that returning will accomplish this? Thanks, PaulHi Paul,
masnu wrote:
but everything I have read says that
The documentation is confusing (I presume you read the documentation? If there's some other source which recommends _endthreadex instead of just
return
ing the control, please ignore it. It's wrong). The documentation could have better been: "Do not call _endthread() or ExitThread() or TerminateThread(), etc., on a thread that was created by a call to _beginthreadex(). That will lead to resource leak. If you have to explicitly "end" a thread that was created by _beginthreadex(), then you should only call _endthreadex(). But just returning from the function is a better alternative." (similarly, _endthread and _beginthread is a pair) It's been explained very elaborately on the book Windows via C/C++ (and probably in a couple of other books too). I remember Dr. Joseph Newcomer had written on this too, which I'm lazy to search for now.masnu wrote:
Is it safe to assume that returning will accomplish this?
Yes! The resource cleanup will happen perfectly if you simply return the control. To conclude, there's simply no reason to call _endthreadex (or any of their akin functions that explicitly "ends" a thread) from your code. Returning the control is the best thing to do! Let me know if you need more information.
“Follow your bliss.” – Joseph Campbell
-
Hi Rajesh, I know that returning from a thread will call
_endthreadex()
but everything I have read says that "terminating a thread with a call to endthread or _endthreadex helps to ensure proper recovery of resources allocated for the thread". Is it safe to assume that returning will accomplish this? Thanks, PaulOK, I just searched Dr. Joseph's article and it's here: http://www.flounder.com/badprogram.htm#AfxExitThread[^] (He's a man that has earned a lot of respect in the community, and he says "Doing this will result in erroneous programs"). Now it's 12.59 AM here and I better hit the bed or I'll wake up late. Good night (probably good day) to you! :)
“Follow your bliss.” – Joseph Campbell
-
OK, I just searched Dr. Joseph's article and it's here: http://www.flounder.com/badprogram.htm#AfxExitThread[^] (He's a man that has earned a lot of respect in the community, and he says "Doing this will result in erroneous programs"). Now it's 12.59 AM here and I better hit the bed or I'll wake up late. Good night (probably good day) to you! :)
“Follow your bliss.” – Joseph Campbell