Today I reviewed the code I wrote a long time ago and found that I wrote such a code,
M_htheard = createthread (null, 0, regpolicyproc, lpvoid (this), 0, null );
Closehandle (m_htheard );
Suddenly confused. Why did I close the thread I just created? I was not very steadfast in getting started and didn't think about it. Now I can't remember it. By checking the information, I solved my doubts.
1. Different from the thread handle (handle), a thread is the workflow of a program, and a thread handle is a kernel object. The life cycle of a thread is that the thread function is executed from the beginning to the end of the thread. Once the thread handle is returned by createthread, closehandle can be used if you do not need it to operate the thread or wait for operations such as waitforsingleobject.
(PS: For a thread, if the thread is in the running state, it is in the non-signal State and there is a signal state after exiting. So we can use waitforsingleobject to wait for the thread to exit)
2. After createthread, You need to perform some operations on this thread, such as changing the priority, waiting by other threads, and forcing termatethread. Then, you need to save the handle and operate closehandle after it is used up.
3. closehandle only closes a thread handle object, indicating that I no longer use the handle, that is, it does not intervene in the thread corresponding to the handle, and has nothing to do with the end thread. If closehandle is not called after the thread is executed, the kernel object may be leaked during the process execution, which is equivalent to the handle leakage, but different from the memory leakage, this will inevitably have a negative impact on system efficiency to a certain extent. However, when the process stops and exits, the system automatically cleans up these resources.
4. Close a kernel object. This includes file, file ing, process, thread, security, and synchronization objects. After createthread is successful, a handle of hthread will be returned, and the count of the kernel object is increased by 1. After closehandle, the reference count is reduced by 1. When it changes to 0, the system deletes the kernel object.
Make some conclusions for knowledge accumulation.