Four methods to end a thread in Windows

Source: Internet
Author: User

In Windows, the created thread has the following four termination methods:

1. thread function return

When a thread function returns, the thread is terminated. This method should always be used to end the running of the thread, because this is the only way to ensure that all thread resources are correctly cleared. If the thread returns successfully in this way, then:

1) All C ++ objects created in thread functions can be correctly revoked through their destructor

2) the operating system correctly releases the memory used by the thread running stack.

3) The system exits the thread.Code(Maintained in the kernel object of the thread) set as the return value of the thread function

4) The system will decrease the usage count of the thread Kernel Object

 

2. exitthread Function

You can call this function to forcibly terminate the running of the thread. This function will cause the operating system to clear all operating system resources used by this thread. However, C ++ resources (such as class objects) are not revoked. This method is usually a function used by windows to cancel a thread. Therefore, it is best to use method 1 to exit the thread, instead of calling this function to return.

 

3. terminatethread Function

Calling this function can also terminate the thread operation. However, unlike the exitthread function, the latter always revokes the called thread, while the former can cancel any thread. TerminatethreadIt is a function that runs asynchronously, that is, it tells the system that you want the thread to terminate the operation. However, when the function returns, the thread cannot be undone.If You Need To Know exactly that the thread has been terminated, you must call waitforsingleobject or similar functions. When methods 1 and 2 are used to cancel a thread, the memory stack space of the thread is also revoked. If terminatethread is used, the system does not cancel the running stack of the thread before the process that owns the thread stops running.

 

4.Process Termination thread

When a process is terminated, all threads in the process are terminated. Because the entire process has been disabled, all resources used by the process must have been cleared. Of course, this includes the stack space of all threads. UseThe exitprocess and terminateprocess functions force cancel the remaining threads in the process, just like calling terminataprocess from each remaining thread,Therefore, this also means that the application is correct.ProgramClearing does not happen, that is, the C ++ object destructor is not called, and the data is not transferred to the disk.

 

To sum up the above four methods, we should always use the first method to terminate the thread operation.

 

 

We can send a message to the subthread when the main thread is about to exit, or tell the subthread to exit automatically.

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.