Can I call CloseHandle() immediately after _beginthreadex() succeeded? Can I call CloseHandle() immediately after _beginthreadex() succeeded? multithreading multithreading

Can I call CloseHandle() immediately after _beginthreadex() succeeded?


Yes, you can close the handle as soon as you decide you no longer need that handle. That won't affect thread execution. However you likely should check whether the thread has been started at all before you proceed.

The leaks you're concerned about are not memory leaks, they are system resources leaks - usually they are much worse.


According to MSDN, you should not close the handle returned by __beginThreadEx:_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. (see http://msdn.microsoft.com/en-us/library/kdzttdcb(ar-sa).aspx for details.)