How to terminate a thread in C ++ 11?

I do not need to terminate the thread correctly or respond to the terminate command. I am interested in terminating a thread using pure C ++ 11.

+108
c ++ multithreading c ++ 11
Aug 31 '12 at 1:10
source share
5 answers
  • You can call std::terminate() from any thread, and the thread you are referring to will end badly.

  • You can arrange ~thread() to execute the target thread object without intermediate join() and detach() on that object. This will have the same effect as option 1.

  • You can create an exception that has a destructor that throws an exception. And then streamline the target thread to throw this exception when it should be forcibly terminated. The hard part of this question is getting the target thread to throw this exception.

Options 1 and 2 do not leak resources within the process, but terminate each thread.

Option 3 is likely to be a resource leak, but is partially compatible with the fact that the target thread must agree to eliminate the exception.

In C ++ 11 (what I know) there is no portable way to inoperatively kill one thread in a multi-threaded program (i.e. without destroying all the threads). There was no motivation to develop such a function.

A std::thread can have this member function:

 native_handle_type native_handle(); 

You might be able to use this to call an OS dependent function to do what you want. For example, on Apple OS, this function exists, and native_handle_type is pthread_t . If you are successful, you will most likely be a resource drain.

+112
Aug 31 '12 at 1:32
source share

@Howard Hinnant answer is correct and comprehensive. But this can be misunderstood if it reads too fast, because std::terminate() (the whole process) has the same name as the "termination" that @AlexanderVX had in mind (1 thread).

Summary: "terminate 1 thread + force (the target thread does not interact) + pure C ++ 11 = no."

+34
Dec 12 '13 at 14:53
source share

This question actually has a deeper character and a good understanding of the concepts of multithreading in general will give you an idea about this topic. In fact, there is neither a language, nor any operating system that will provide you with the ability to asynchronously interrupt a stream without warning, so as not to use them. And all these runtimes strongly advise the developer or even require the creation of multi-threaded applications based on joint or synchronous thread interruption. The reason for these common decisions and tips is that they are all built on the basis of the same common multithreaded model.

Let's compare the concepts of multiprocessing and multithreading in order to better understand the advantages and limitations of the second.

Multiprocessing involves dividing the entire runtime into many completely isolated processes controlled by the operating system. The process includes and isolates the state of the runtime environment, including the local memory of the process and the data inside it and all system resources, such as files, sockets, synchronization objects. Isolation is a critical process characteristic because it limits the spread of errors to the boundaries of the process. In other words, no process can affect the consistency of any other process in the system. The same is true for process behavior, but in a less limited and more blurry way. In such an environment, any process can be killed at any โ€œarbitraryโ€ moment, because, firstly, each process is isolated, and secondly, the operating system has full knowledge of all the resources used by the process, and can release all of them without leakage , and finally, the process will not be killed by the OS at any time, but in the number of clearly defined points where the state of the process is well known.

In contrast, multithreading involves running multiple threads in a single process. But all these threads share the same unit of isolation, and there is no control over the operating system's internal state of the process. As a result, any thread can change the state of the global process, as well as ruin it. At the same time, the points at which the state of the stream is well known that it is safe to kill the stream are completely dependent on the application logic and are not known either for the operating system or for the programming language. As a result, interruption of a stream at an arbitrary moment means its destruction at an arbitrary point of its execution path and can easily lead to data loss throughout the organization, memory and handle leakage, thread leakage and locking blocks and other synchronization primitives inside the process, a closed state that prevents passage other threads.

In this regard, the general approach is to force developers to implement synchronous or joint completion of the flow, when one thread can request another completion of the flow, and another thread at a clearly defined point can check this request and begin the shutdown procedure from the well - a certain state with safe and consistent release of all global resources of the entire system and local resources of the process.

+8
Feb 19 '15 at 10:39
source share

Tips for using an OS-dependent function to terminate a C ++ stream:

  1. std::thread::native_handle() can receive threads of a valid native descriptor type before calling join() or detach() . After that, native_handle() returns 0 - pthread_cancel() will execute coredump.

  2. To effectively call your own thread termination function (for example, pthread_cancel) , you need to save your own descriptor before calling std::thread::join() or std::thread::detach() . So your native terminator always has a valid native handle to use.

Further explanations can be found at : http://bo-yang.imtqy.com/2017/11/19/cpp-kill-detached-thread .

+6
Nov 20 '17 at 18:18
source share

I assume that the thread to be killed is either in any standby mode, or is doing some kind of hard work. I would suggest using the "naive" way.

Define some global logical:

 std::atomic_bool stop_thread_1 = false; 

Put the following code (or similar) at several key points so that it causes all functions in the call stack to return before the thread ends naturally:

 if (stop_thread_1) return; 

Then, to stop the thread from another (main) thread:

 stop_thread_1 = true; thread1.join (); stop_thread_1 = false; //(this can be when starting the thread instead) 
0
May 27 '19 at 14:48
source share



All Articles