How can I go about emulating the pthread_kill() function with C++ threads? I asked a question about this earlier but there was no response to it. Will the thread::native_handle() function help here?
Asked
Active
Viewed 2,147 times
6
-
You shouldn't mix the two apis. Have a look at this [thread](http://stackoverflow.com/questions/13531241/cancelling-stdthread-using-native-handle-pthread-cancel). – Mohamad Elghawi Nov 19 '15 at 15:23
-
Also, read [this](http://stackoverflow.com/questions/2790346/c0x-thread-interruption) on thread interruption. – Mohamad Elghawi Nov 19 '15 at 15:24
-
I'm not trying to cancel the thread. No object destruction problems should occur here. I am simply trying to signal a thread – Curious Nov 19 '15 at 15:28
-
Then I can't see why you can't use [std::thread::native_handle](http://en.cppreference.com/w/cpp/thread/thread/native_handle). Have a look at that example. It calls `std::thread::native_handle` and passes the result to `pthread_setschedparam`. Best thing to do is to give it a try. – Mohamad Elghawi Nov 19 '15 at 15:33
-
Yep I got it to work. I was just wondering if there is some other object oriented C++ way to do that which I missed – Curious Nov 19 '15 at 15:45
-
The C++ standard must remain cross platform (e.g. signals do not exist on Windows). Anything specific like this and you'll find yourself having to write code using your platforms native api. – Mohamad Elghawi Nov 19 '15 at 15:53
-
Interesting question. I guess you cannot use standard boost mechanism for thread termination because the thread is blocked in some waiting method and you cannot unblock it otherwise. Could be a blocking `read` or decoding a frame in ffmpeg? – Domagoj Prelošćan Nov 19 '15 at 15:57
-
If you want a nice object-oriented solution then write one yourself, it's easy enough to create a class that wraps around std::thread. – Mohamad Elghawi Nov 19 '15 at 15:59
-
Why would you "kill" a thread? What for? – curiousguy Nov 29 '19 at 16:53
1 Answers
6
So the answer to this question is to rely on platform dependent features and use std::thread::native_handle
with pthread_kill()

Curious
- 20,870
- 8
- 61
- 146