Thread termination in Windows

Source: Internet
Author: User

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

Related Article

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.