Reputation: 16987
I want to wait for a pthread condition variable, but when I get a SIGUSR1 (or any other signal) I want to stop waiting and detect that it stopped waiting because of a signal, not because of a pthread_cond_signal or spurious wakeup. How can I do this?
Upvotes: 0
Views: 720
Reputation: 239011
A reliable way to handle signals in a pthreads program is to mask all the signals you wish to handle in every thread, and create a dedicated signal handling thread that loops around calling sigwaitinfo()
(or sigtimedwait()
).
The signal handling thread can then use ordinary mutex-protected shared variables and pthread_cond_signal()
/ pthread_cond_broadcast()
wakeups to inform other threads about the signals received.
In your example, the dedicated signal handling thread could set a (mutex-protected) flag indicating that SIGUSR1
has been received, then signal the condition variable that your thread is waiting on. The waiting thread just needs to check the signal flags in addition to its other shared state in the loop around pthread_cond_wait()
.
Upvotes: 1