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?