加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

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

发布时间:2020-12-15 00:07:52 所属栏目:Java 来源:网络整理
导读: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 ar
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()返回之后处理信号处理线程的正常执行流程中的信号.这允许它访问非异步信号安全功能,这是整体.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读