Why must I use closehandle to close the handle?

Source: Internet
Author: User

Many programs are written like this in the creation thread:

Threadhandle = createthread (null, 0 ,.....);

Closehandel (threadhandle );

1. The thread and the thread handle are not a single thing. The thread runs on the CPU... (not clear). The thread handle is a kernel object. We can use a handle to operate the thread, but the thread's lifecycle is different from that of the thread's handle. The life cycle of a thread is from the execution of a thread function to the Return. the life cycle of a thread handle is returned from createthread to your closehandle ().

2. A thread is a kernel object. The system maintains each kernel object. When the reference count of each kernel object is 0, the system releases the object from the memory, closehandle reduces the reference count of the thread object by 1. All kernel objects (including thread handle) are system resources and need to be paid back when they are used up. That is to say, closehandle must be disabled after use. Otherwise, your system's handle resources will soon be used up.

3. If you need to perform operations on this thread after createthread, such as changing the priority, waiting by other threads, and forcing termatethread, you need to save the handle and use it in closehandle. If you open a thread without interfering with it, createthread and then directly closehandle.

So closehandel (threadhandle );

If a thread handle object is closed, it means that I no longer use the handle, that is, I do not intervene in the thread corresponding to the handle. There is no end thread.

If you think there is one more variable, you can also write it:

Closehandel (createthread (null, 0 ,.....));

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.