CreateThread () and immediately after the CloseHandle () question

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?

================================================

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.

================================================

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,.....));

================================================================

In Windows core programming, calling CloseHandle (HANDLE) means that the creator discards operations on the kernel object. If the object has a reference object count of 0, undo the object.

====================================================================

It is good practice to call CloseHandle () immediately after a thread is created, which does not affect the execution of threads, because even if you close the handle, its internal count is not 0. But if you don't, the thread object will be stuck in memory after the threads end, which means you have handle leak.
Returning this handle to you is to give you the opportunity to implement external actions on this thread, such as WaitForSingleObject.

==================================================================

The function of CloseHandle is to close an open object handle, which can be a thread handle or a handle to other kernel objects such as process, semaphore, and ExitThread function is to terminate a thread that accepts an argument that is a thread's exit code.
By calling CloseHandle, you can tell the system that you have completed an operation on a kernel object that first checks the handle table of the calling process to see if the process has access to the object that the handle is pointing to, and returns False if the handle is invalid, if valid, The system will get the address of the data structure of the kernel object, reduce the use Count member in the structure by 1, and if the count becomes 0, the kernel object will be released from the kernel.
If the count is less than 0, it means that there are other processes using the kernel object and it will not be released.
ExitThread is the recommended way to end a thread, and when called, the current thread's stack is freed and the thread terminates, which, as opposed to the TerminateThread function, completes the cleanup of the DLL attached to the thread.
If you need further information, you can refer to:
Http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/handobj_289x.asp
and http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/prothred_531g.asp
And so on MSDN information.

==================================================================

When a thread is created as a resource and not just created as a thread reference, I think the system itself will have a reference to manage the thread, so the reference count will not be zero after the user thread releases the thread handle. Reference counting is a mechanism of resource self-management, and the resource itself is counted as a reference zero know that others no longer need their own, and thus kill themselves.

=================================================================

CreateThread after the thread's reference count is not 1, call CloseHandle just say that they are not interested in this thread, the thread is still working

=================================================================

The reference count for that thread after CreateThread is not 1, but 2.
Creating a new process causes the system to create a process kernel object
and a thread kernel object. At creation time, the system gives each object
An initial usage count of 1. Then, just before CreateProcess returns, the
function opens the Process object and the thread object and places the
Process-relative handles for the hprocess and Hthread members of
The process_information structure. When CreateProcess opens these objects
Internally, the usage count for each becomes 2.

=================================================================

After the new process is created, the count is initialized to 1, and the function needs to return the handle of the process kernel object, which is equivalent to opening a newly created class-core object, counting plus 1

------------------------------------

CloseHandle function

BOOL CloseHandle (

HANDLE Hobject

);

Parameters

Hobject: Represents an open object handle.

return value

TRUE: Execution succeeded;

FALSE: Execution failed, you can call GetLastError () to learn the cause of the failure.

Function Description:

Closes a kernel object. These include files, file mappings, processes, threads, security, and synchronization objects. After CreateThread succeeds, a hthread handle is returned, and the count of the kernel object is 1,closehandle, the reference count is reduced by 1, and the kernel object is deleted when it becomes 0 o'clock.

If the thread is finished, no call to CloseHandle, during the process execution, will cause the kernel object leakage, the equivalent of the handle leakage, but different from the memory leak, this will have a certain degree of negative impact on the efficiency of the system. However, when the process finishes exiting, the system automatically cleans up these resources.

From:http://blog.csdn.net/lhsxsh/archive/2009/02/18/3905505.aspx

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.