Introduction to win32 Thread Management
The CloseHandle Function
The CloseHandle function when called with a thread decrements the reference count of the thread kernel object. When the count reaches zero, the object itself is freed. The default reference count for a thread object is two, sibce the thread itself has a reference to the kernel object, and the HANDLE returned from the CreateThread function does as well. When we call the CloseHandle function the reference count drops, and when the thread itself terminats the count also drops. Both are required for the thread kernel object to be destroyed.
The CreateThread function returns a HANDLE that is local to the process. This handle refers to a kernel object that is managed by KERNEL32.DLL. The handel points to something that our program is not allowed to access directly, for security and system integrity reasons.
We can use the GetExitCodeThread function to get the status of a thread. We supply the HANDLE to the kernel object and a pointer to a DWORD variable to store the status.
The GetExitCodeThread function returns a Boolean value indicating whether or not it succeeded. If the thread is still active, the value stored in the DWORD variable will be STILL_ACTIVE.
We can suspend and resume threads using the SuspendThread and ResumeThread functions.