Reputation: 1556
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?
Upvotes: 1
Views: 1745
Reputation: 11232
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()
.
Upvotes: 0
Reputation: 84169
You are probably looking for pthread_sigqueue(3)
called from the main thread, and sigwait(3)
or sigtimedwait(2)
in the child thread(s).
Upvotes: 1