Linux编程接口书提到了一种在多线程程序中处理异步信号的方法:
- 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()
or
sigwait()
.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.
现在的问题:
>当内核想要传递信号时,它会选择进程任意的一个线程.从哪里可以知道将信号传递给专用线程?
> pthread API是非同步安全的功能.那么我们如何在信号处理程序中使用它们呢?