views:

53

answers:

1

I'm reading the documentation about _beginthreadex and _endthreadex but there are a few things I don't understand.

Note that the documentation keeps documenting the "extended" and normal functions at the same time, but I'm not using _beginthread and _endthread; only their extended versions.


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. Terminating a thread with a call to endthread or _endthreadex helps to ensure proper recovery of resources allocated for the thread.

  • If _endthreadex is called automatically, how come calling it helps to ensure "proper recovery of resources"? It shouldn't make any difference whether I call it or not, or does it?

_endthread automatically closes the thread handle (whereas _endthreadex does not). Therefore, when using _beginthread and _endthread, do not explicitly close the thread handle by calling the Win32 CloseHandle API.

  • If _endthreadex does not close the handle, how come I shouldn't close it with CloseHandle?
  • All of my threads only voluntarily terminate by returning from their main function and are never forcefully terminated. According to the documentation, when this happens _endthreadex is called automatically.

    This though won't close the handle. Assuming that I do need to close it, notwithstanding with what said above, how can I do that since at this point the thread is dead? Should I somehow close it from another thread? What happens if I leave it open?

+4  A: 

If _endthreadex is called automatically, how come calling it helps to ensure "proper recovery of resources"? It shouldn't make any difference whether I call it or not, or does it?

I think they meant this for the cases when you do not use a standard way of terminating your thread.

If _endthreadex does not close the handle, how come I shouldn't close it with CloseHandle?

You should close it with CloseHandle when using _endthreadex. The documentation says that only _endthread closes the handle (and therefore a CloseHandle call is superfluous).

All of my threads only voluntarily terminate by returning from their main function and are never forcefully terminated. According to the documentation, when this happens _endthreadex is called automatically.

Closing the thread handles from the thread that started it is a commonly used solution. You should remember the handle and in appropriate place wait for the thread to finish (using WaitForSingleObject) and then close its handle. If you don't do that you will cause a leak of resources. That is usually not a big problem if you have a few threads but it is definitely not a good practice.

dark_charlie
What happens if I close the handle in the thread's main function?
Andreas Bonini
I guess that's an undefined behavior. It might work, it might leak, it might deadlock, it might crash. And worst of all - it can change any time and vary between different computers.
dark_charlie