Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emulate pthread_kill with C++ threads

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?

like image 600
Curious Avatar asked Nov 19 '15 15:11

Curious


People also ask

How do I run a thread program?

To execute the c file, we have to use the -pthread or -lpthread in the command line while compiling the file. Syntax: int pthread_create(pthread_t * thread, const pthread_attr_t * attr, void * (*start_routine)(void *), void *arg);

How do you send a signal to a thread?

The pthread_kill() function sends the signal sig to thread, a thread in the same process as the caller. The signal is asynchronously directed to thread. If sig is 0, then no signal is sent, but error checking is still performed; this can be used to check for the existence of a thread ID.

How do you kill a thread in C++?

You could call std::terminate() from any thread and the thread you're referring to will forcefully end. You could arrange for ~thread() to be executed on the object of the target thread, without a intervening join() nor detach() on that object.

What is thread Native_handle?

thread::native_handleReturns the implementation defined underlying thread handle.


1 Answers

So the answer to this question is to rely on platform dependent features and use std::thread::native_handle with pthread_kill()

like image 170
Curious Avatar answered Nov 09 '22 01:11

Curious