简体   繁体   中英

POSIX Threads and signal masks

Is there a way to change the signal mask of a thread from another thread? I am supposed to write a multithreaded C application that doesn't use mutex, semaphores and condition variables, only signals.

So it would look like something like this: The main Thread sends SIGUSR1 to its process and and one of the 2 threads (not including the main thread), will respond to the signal and block SIGUSR1 from the sigmask and sleep. Then the main thread sends SIGUSR1 again, the other thread will respond, block SIGUSR1 from its sigmask, unblock SIGUSR1 from the other threads sigmask, so it will respond to SIGUSR1 again.

So essentially whenever the main thread sends SIGUSR1 the two other threads swap between each other.

Can somebody help?

您可能正在寻找从主线程调用的pthread_sigqueue(3) ,以及子线程中的sigwait(3)sigtimedwait(2) )。

A thread can only modify its own signal mask (via pthread_sigmask() ). If you want to modify another thread's signal mask, you will have to write code yourself to ask the other thread to do it.

Signals are being sent to the process here, so kill() or sigqueue() are the functions to use. The latter will avoid coalescing multiple signals together which may happen with kill() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM