Threads in Windows can be terminated in the following four methods.
1. Return the thread function. (Strongly recommended)
2. The thread "kills" itself by calling the ExitThread function. (Avoid using)
3. The TerminateThread function is called by a thread in the same process or another process (avoid using it)
4. Process containing threads stops running (avoid using)
1) thread function return (natural death)
Cleanup includes: the C ++ object is destroyed correctly by the destructor, the thread stack is released correctly, the return value of the thread function is set to exit code, and the thread Kernel Object reference count is reduced.
2) Call the ExitThread function to commit suicide)
In addition to releasing C/C ++ resources (calling the destructor to destroy the C ++ object. Therefore, avoid using this method.
3) Call the TerminateThread function (this is a type of murder if the user commits suicide or kills him unconsciously)
Call TerminateThread to kill the thread. The terminated thread cannot receive the "kill" Notification (DLL_THREAD_DETACH notification). Therefore, the thread cannot be properly cleaned up and cannot stop itself from being terminated.
In addition to releasing C/C ++ resources (calling the destructor to destroy the C ++ object), the cleanup operation will not destroy the thread stack, unless the process containing this thread stops, the reason for this is to allow other running threads to reference the value on the stack of the thread that is "killed. The rest are the same as above. Therefore, avoid using this method.
PS: most of the above summary comes from Windows core programming 5th