Thread and thread handle

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.