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

4.6k views Asked by At

I'm not interested in using the handle returned from _beginthreadex(). Is it safe to call CloseHandle() on it immediately?

I believe this must be done to avoid memory leaks.

2

There are 2 answers

2
sharptooth On BEST ANSWER

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.

1
YoungLearner On

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.)