Use of the CloseHandle () function (closing the thread handle)

Source: Internet
Author: User

Many programs write this in the creation thread:

............

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

Closehandel (Threadhandle);

。。。。。

This is not just created and closed again. How does the thread work?

For:

Closing A thread handle does not terminate the associated thread. To remove a thread object, you must terminate the thread, then close all handles to the thread.

Quote from MSDN

1, Thread and line Cheng (Handle) is not a thing, the thread is running on the CPU ... (It's not clear), the thread handle is a kernel object. We can manipulate threads through handles, but the life cycle of threads is different from the lifecycle of thread handles. The life cycle of a thread is the thread function from the start to the return, and the thread handle's lifecycle is returned from CreateThread to your CloseHandle ().

2, all the kernel objects (including thread handle) are system resources, used to return, that is, after use must be CloseHandle closed, if not to do so, your system's handle resources quickly ran out.

3, if you createthread later need to do some operations on this thread, such as the change of priority, by other threads waiting, forced termatethread and so on, it is necessary to save this handle, used in CloseHandle. If you open a thread and do not need to intervene on it, CreateThread directly closehandle the line.

So

Closehandel (Threadhandle);

Just closed a thread handle object, which means that I no longer use the handle, that is, no interference to the thread corresponding to the handle. There is no end thread.

If you think you have one more variable, you can write it as:

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

Article Source: DIY Tribe (http://www.diybl.com/course/3_program/c++/cppjs/20090208/155031.html)

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.