Must I CloseHandle() on a thread handle?

丶灬走出姿态 提交于 2019-11-30 05:32:43

问题


_beginthreadex returns a handle to a thread:

m_hStreamStatsThread = (HANDLE) _beginthreadex( NULL, 0, StreamStatsThread, this, 0, NULL );

This handle may be used if you need to refer to the thread in calls like TerminateThread(..) for example.

According to the MSDN page on _beginthreadex, _beginthreadex won't always return a valid handle - e.g. it may also return -1L on error etc.

When a thread has completed normally, do I have to call CloseHandle on the thread handle, or can I just set its value to NULL / INVALID_HANDLE_VALUE?


回答1:


Agree with Nemanja Trifunovic.

Even after the thread exited - its handle is valid. You can for instance query its return value.

As a general rule: every Win32 handle must be closed by CloseHandle, unless otherwise specified.




回答2:


The code sample on the MSDN page you posted a link to includes a call to CloseHandle(). Setting the handle's value to NULL does not decrease the kernel object's internal ref count and is pretty much useless anyway.



来源:https://stackoverflow.com/questions/3959473/must-i-closehandle-on-a-thread-handle

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