在多线程程序中处理异步信号

前端之家收集整理的这篇文章主要介绍了在多线程程序中处理异步信号前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
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是非同步安全的功能.那么我们如何在信号处理程序中使用它们呢?

解决方法

当内核提供一个进程导向的信号时,它选择一个没有信号阻塞的线程.这意味着它不会从信号处理线程中选择任何线程(它的作用就像在sigwaitinfo()中被阻塞的信号被解除阻塞)或类似的信号.换句话说:内核知道传送信号的位置,因为你已经安排了这样的事情,使得信号处理线程是允许传递信号的唯一线程.

您不使用pthreads API或信号处理程序中的任何非异步信号安全功能.所概述的解决方案不处理信号处理程序中的信号 – 它在sigwaitinfo()返回之后处理信号处理线程的正常执行流程中的信号.这允许它访问非异步信号安全功能,这是整体.

猜你在找的Java相关文章