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

痞子三分冷 提交于 2019-12-07 02:05:51

问题


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.


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/8241712/can-i-call-closehandle-immediately-after-beginthreadex-succeeded

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!