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

前端 未结 3 1778
时光取名叫无心
时光取名叫无心 2020-12-17 05:44

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 (N

3条回答
  •  天涯浪人
    2020-12-17 06:14

    There is no way to non-cooperatively stop a thread from running with standard C++. That doesn't mean it's impossible, but you may need to go back to your systems native handle.

    For a standard conforming way you can use synchronization primitives (e.g. an std::atomic) to set a kill flag from the outside and read it within the thread. But it still has to be the thread that finishes on its own.

提交回复
热议问题