Four ways to exit a thread in C + + _c language

Source: Internet
Author: User

There are four ways to exit a thread:

1. Return of the thread function (preferably):
It is returned with the return of the thread function, and the terminating thread is the safest, and after the line function return returns, the class object that is requested in the function is cleaned, that is, the destructor of those objects is called. The _endthreadex () function is then automatically invoked to clean up the _beginthreadex (...). The resource that the function requests (primarily the Tiddata object that is created).

2. Call the _endthreadex () function or the ExitThread () function (preferably not):
If you exit the thread using both methods, the thread function's return statement is not executed, so the destructor of the class object requested in the thread function scope is not invoked, causing a memory leak.

The remaining two kinds of programming must be avoided.
3. Invoke the TerminateThread () function with another thread in the same process (must be avoided);
4. Terminate the process in which the thread is located (absolute avoidance);

But to say that _endthreadex is completely useless is certainly wrong, _endthreadex is not an outdated function, and proper use does not cause problems.
For example, in the main function of a thread, return is a good substitute for _endthreadex, just as the return in the main function is a good alternative to exit () or exitproccess (), but this does not mean that the exit function is useless. For example, a thread calls a child function, and if the child function decides to exit the thread, return is useless, _endthreadex can terminate the thread.

But this design is not good because it may cause a resource leak from the LZ. Especially considering that the resource leaks after the end of the background thread are more deadly than the main thread's resource leaks (after the main thread exits, the process exits, the OS cleans up all the resources, doesn't matter if the leak doesn't leak, and when the child threads exit, the main thread may run for a long time, and there may be a large number of the same type of child threads exiting, causing deadly leaks.
Good design or return the main function of the thread, let ThreadProc to decide is not to exit, in this sense, _endthreadex not necessary. Microsoft also pointed out that some programmers are to call the Exit series functions (exitthread,exitproccess, etc.), have no choice but to provide.

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.