The Linux Programming Interface Book has mentioned a method for dealing with asynchronous signals in a multi threaded program:
- All threads block all of the asynchronous signals that the process might receive. The simplest way to do this is to block the signals in the main thread before any other thread are created. Each subsequently created thread will inherit a copy of the main thread's signal mask.
- create a single dedicated thread that accepts incoming signals using
sigwaitinfo()
,sigtimedwait()
orsigwait()
.The advantage of this approach is that asynchronously generated signals are received synchronously. As it accepts incoming signals, the dedicated thread can safely modify shared variables (under mutex control) and call non-async-safe functions. It can also signal condition variables, and emply other thread and process communication and synchronization mechanisms.
Now the questions:
- when kernel wants to deliver signals it choose one of the threads inside process arbitrary. from where it can know to deliver signal to the dedicated thread?
- pthread API is non-aync-safe functions. so how we can use them inside signal handler?