POSIX provides the function pthread_cancel to cancel thread execution but seems C++ does not provide a such function Also is it possible to use a mix std::thread and pthread_cancel ?? I also see on linux system , std::thread is a wrapper of POSIX thread
-
1relevant: https://stackoverflow.com/questions/13531241/cancelling-stdthread-using-native-handle-pthread-cancel – Bas Groothedde May 25 '21 at 08:24
-
1Depends on what your thread does. if it is a worker thread (i.e. it waits for work and runs in a loop) you can add a boolean to each thread that signals the thread that is should terminate. You can then call `thread::join()` – Raildex May 25 '21 at 08:25
-
1@Raildex indeed, that's the best way; however that is not cancellation of the thread, that actually finishes the thread correctly. OP wants to cancel the thread like `pthread_cancel`, which is not portable. – Bas Groothedde May 25 '21 at 08:26
-
1@BasGroothedde "Cancelling a thread" sounds something I'd consider code smell – Raildex May 25 '21 at 08:28
-
1@Raildex I agree, I think your own logic should cause a thread to finish. Cancelling or killing a thread sounds bad. – Bas Groothedde May 25 '21 at 08:29
-
I saw your edit asking about mixing after placing an answer to your original question. If you're accurate and it's simply a wrapper you should be able to mix them. I'd say give it a try, that's probably faster than waiting for someone who knows. – Daniël van den Berg May 25 '21 at 08:33
-
In my project, legacy codes used the POSIX thread. Now I want to migrate it to C++11 starting with std::thread. It turns out not easy. I guess the change to logic is not trivial – nhatnq May 25 '21 at 08:39
-
There are very, very few uses of pthread cancel that are not crash-roulette. I doubt your "random" existing code base is one of them. Plausibly it is "use tested" where lots of historical tweaks and bug fixes make the crashes rare, but if you are doing non trivial refactoring all of that is thrown out the window. – Yakk - Adam Nevraumont May 25 '21 at 10:58
1 Answers
Although there is no exact replacement for pthread_cancel, you can come close with jthread.
The class jthread represents a single thread of execution. It has the same general behavior as std::thread, except that jthread automatically rejoins on destruction, and can be cancelled/stopped in certain situations.
This will not interrupt the thread, but still relies on "cooperative" mechanisms. The running jthread will have to keep polling it's stop token and stop by itself.
A good example can be found here.
std::jthread sleepy_worker([] (std::stop_token stoken) {
for(int i=0; i < 10; i++) {
std::this_thread::sleep_for(300ms);
if(stoken.stop_requested()) {
std::cout << "Sleepy worker is requested to stop\n";
return;
}
std::cout << "Sleepy worker goes back to sleep\n";
}
});
sleepy_worker.request_stop();
sleepy_worker.join();
But to bluntly answer your question: no, there is (currently, c++20) no portable way of cancelling a thread.
P.S.: Actually "killing" a thread (or anything for that matter) is something that should usually be avoided. As such I personally doubt you'll ever see a mechanism for it in ISOC++.

- 2,197
- 1
- 20
- 45
-
Related answer: https://stackoverflow.com/a/56328281/518879 – Melroy van den Berg Dec 28 '21 at 00:29