2

I’ve been looking for an answer, and indeed found some possible ways to stop a running parallel thread with C++, but the solutions would usually apply only on Windows (for instance, the TerminateThread() function ). Is there a way to stop a running parallel thread on Mac (I’m using CodeBlocks) ?

Wojtek
  • 21
  • 2
  • 1
    How did you create your thread? – indiv Aug 10 '15 at 21:30
  • I just used the 'std::thread’ function. – Wojtek Aug 10 '15 at 21:36
  • 1
    Are you asking how to communicate to another thread that it should stop? Or are you asking for the memory leaks and corrupted data that [`TerminateThread()` normally causes](http://stackoverflow.com/questions/1702172)? – Drew Dormann Aug 10 '15 at 21:42
  • Use the `native_handle` method and then call your native API. – jxh Aug 10 '15 at 21:43
  • 3
    You shouldn't want to use `TerminateThread`. You want to set up something to signal to your thread function to exit cleanly. This can be done a dozen ways. – David Aug 10 '15 at 21:43
  • @DrewDormann I’m asking about the first one : how to communicate to a thread to stop. – Wojtek Aug 10 '15 at 21:47

1 Answers1

4

A typical clean/safe setup might be....

std::atomic<bool> exit{false};

std::thread thread([&]{
    while (!exit) { /* do stuff */ }
});

// later, when you want to exit:
exit = true;

// `join` before the `thread` object goes out of scope
thread.join();

From this you can probably see there are endless ways to tell your thread to stop running and end cleanly. Just make sure whatever you way you use is thread safe (either atomic or protected by a mutex) and make sure you call thread.join() before the thread object goes out of scope, or any time you wish to block waiting for the thread to finish.

David
  • 27,652
  • 18
  • 89
  • 138
  • To clarify what was said above: you *never* terminate a thread directly using the system API, as this is dangerous and can result in corrupted data (many systems don't even include a call to explicitly terminate a thread for this reason). – SevenBits Aug 10 '15 at 22:02
  • Thanks for the answer ! However, is there a library to include to use the 'std::atomic’ ? I’m getting the following error from using it… : "no member named 'atomic' in namespace 'std'; did you mean 'atoi’? "| – Wojtek Aug 10 '15 at 22:02
  • @Wojtek: You need to `#include `. – Jerry Coffin Aug 10 '15 at 22:08
  • @JerryCoffin: Strangely I’m getting this error : “ is not implemented “... – Wojtek Aug 10 '15 at 22:11
  • @Wojtek you can use `std::mutex` to guard a regular `bool` instead of an atomic bool if you need to. – David Aug 10 '15 at 22:42
  • @Wojtek, if you don't have , I guess you are not using C++11. – JWWalker Aug 10 '15 at 23:14
  • @JWWalker Or their tool chain doesn't provide full C++11 library support. Considering they're using `std::thread` I imagine that's the case. – Captain Obvlious Aug 10 '15 at 23:42
  • @Dave Thanks for the insights, however it seems it’s still not working ! When I implement your code using mutex instead of atomic, my CodeBlocks is still unsatisfied, here are the errors I’m getting : "error: expected unqualified-id" for std::mutex exit{false}; and “error: expected expression” for std::thread thread([&]{ – Wojtek Aug 11 '15 at 07:02
  • @Wojtek you need to look up and learn about what a mutex is, what an atomic is, and what thread safety is. That's not how you use it. – David Aug 11 '15 at 15:50
  • #Dave Yes I imagine, I’m trying to figure out how mutex works right now… For sure, I’m still learning… Thanks for your help. – Wojtek Aug 11 '15 at 16:05
  • @Dave I managed to implement your solution into my code, using XCode instead of CodeBlocks. It appears that, using your solution, the thread does not stop, unless the code /* do stuff */ in the while loop takes end itself. Put it differently, with your solution I can’t really force the thread to end, unless its content arrives at the end. I hoped I could force the thread to end: the idea is to run the thread for 5 minutes (time being measured with another thread), and stop it after this delay, whether the content of the thread has actually came to the end or not. Any suggestion please? – Wojtek Aug 12 '15 at 07:39
  • @Dave ps: The idea is to run two threads simultaneously, the 1st with the effective program and the 2nd to measure time, and to end the 1st once the 2nd is finished (whether the 1st has or not). Initially I used the thread.detach() function, so I run both threads in the same time, but it does not end the 1st thread, unless the program contained in it has ended itself. (I hope it makes sense to you what I’m saying…) ! Thanks for help ! – Wojtek Aug 12 '15 at 07:48