Proper way to terminate a thread in c++

前端 未结 1 1538
臣服心动
臣服心动 2020-12-19 14:46

I\'m learning about multithreading and I wrote this code:

#include 
#include 
#include 
#include 
#i         


        
相关标签:
1条回答
  • 2020-12-19 15:32

    No. The correct (and only correct in standard C++) way to terminate a thread is to return from its thread function.

    std::terminate kills your entire process. Even if it only killed the current thread (i.e. behaved like the Win32 TerminateThread function, which you should never call!), it would not unwind the stack, not call destructors, and thus possibly leave some necessary cleanup unfinished (like releasing mutexes).

    std::terminate is meant to be used on a critical failure where your program cannot possibly continue. The message "without an active exception" is because the primary use of terminate is to kill the program if the exception system fails, e.g. due to a nested exception, so the function by default looks for an active exception and prints information about it.

    0 讨论(0)
提交回复
热议问题