Why the thread handle is closed and the thread can still run

Source: Internet
Author: User
Tags exit in

As long as the main thread has a threading handle, you can perform certain operations on the thread afterwards, such as querying the thread state and so on, by the handle, without the handle, the system will not know the state of the thread to check. But keeping this handle closed is not necessary for the thread to run.

Closing a thread handle simply frees the handle resource, and if you no longer use its handle, you should close the handle and release the system resources after the new thread is opened. It does not matter whether the thread handle is closed or the end of the thread.

A handle can be thought of as a number assigned by the system to a resource (such as a thread). Close this number, for different resources, the effect is not the same.   For threads, closing this number does not mean terminating the thread, but it is difficult to manipulate it later.   This is like take off the house number, do not hurt this family, but later to this letter is troublesome. Also, if the main thread only wants to create threads, and do not want to query or manipulate it later, it is a good habit to close the handle in time, lest the time is not closed, and then forget, then leaked the system handle resources (the total number of system handles is limited).

If you know what the reference count is, it's a good explanation. CloseHandle (hthread) simply subtracts a hthread reference count.

The thread handle is like your home address, the thread is like your house, you say that if there is no address of your home, there is no house of your house? The thread is the same as the thread handle, and the handle can only do something about the thread.

CloseHandle is a closed thread handle that frees thread resources instead of terminating threads. Termination of threads with the use of TerminateThread or exitthread,terminatethread can cause a variety of resource release problems, The main is the various types of DLLs that are connected to the thread, and they cannot get notifications of thread exits. You should naturally exit in the thread (that is, implicitly call ExitThread) or explicitly call ExitThread. The timing of the exit should be implemented by a synchronization object or other mechanism, and the main thread should generally use the WaitForSingleObject function to wait for the thread's handle to ensure that the thread exits.

Why the thread handle is closed and the thread can still run

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.