Search code examples
c++multithreadingc++11stdthread

How to stop an std::thread from running, without terminating the program


I am trying to learn std::threads from C++11 to make a threading system.

I was wondering if there is a way to stop a thread from running (Not sleeping, but really destructing the thread or so to speak) without terminating the whole program.

I know std::join exists, but that forces a thread to wait till all threads return.

Is there another way to handle this? (For example for making a ThreadPool class without having to block a thread?)


Solution

  • The C++ std::thread class is really just a minimal interface layered on top of some more complete implementation-defined threading package. As such, it only defines a tiny amount of functionality -- creating new threads, detach, and join. That's pretty much it -- there's no standard way of managing, scheduling, stopping/starting/killing threads or doing much else.

    There is a native_handle method that returns an implementation-defined type which can probably be used to do what you want, depending on the implementation.