简体   繁体   English

是否可以在进程中的所有线程上设置pthread_sigmask?

[英]Is it possible to set pthread_sigmask on all threads in a process?

This is a follow-up to this question and I have looked at the related questions. 这是该问题的后续措施,我已经研究了相关问题。

I am still attempting to do some cleanup when SIGTERM is received, and then achieve the effect of TERM which is the default behavior when no thread in a process is waiting for a signal, and and no signal handler is defined. 我仍然在尝试做一些清理工作时SIGTERM被接收,然后达到的效果TERM这是当一个进程没有线程在等待信号的默认行为, 并且没有信号处理函数的定义。

In the earlier question, I had assumed that signals had deterministic behavior in a multithreaded application, but after some non-deterministic results and some research, I realized that this is not a safe assumption. 在前面的问题中,我假设信号在多线程应用程序中具有确定性行为,但是经过一些不确定性结果和一些研究之后,我意识到这不是一个安全的假设。

My application is multithreaded and using a normal signal handler via sigaction or signal has non-deterministic results because the arbitrary choice of which thread is actually interrupted to run the signal handler matters to the signal handler's operation (it is cleaning up some threads which might deadlock if the wrong thread is interrupted). 我的应用程序是多线程的,并且通过sigactionsignal使用普通的信号处理程序具有不确定的结果,因为任意中断实际运行该信号处理程序的线程的选择对信号处理程序的操作很重要(它正在清理一些可能导致死锁的线程如果错误的线程被中断)。

Therefore, I switched to doing synchronous signal handling . 因此,我转而进行同步信号处理 In particular, I am blocking SIGTERM using pthread_sigmask in the starting thread, and then calling sigwait() in the thread that will actually perform the cleanup. 特别是,我在起始线程中使用pthread_sigmask阻止了SIGTERM ,然后在实际执行清除操作的线程中调用sigwait() However, after sigwait() returns in the thread, and I finish my cleanup, I then try the following: 但是,在sigwait()返回线程后,完成清理之后,请尝试以下操作:

kill(getpid(), SIGTERM);

However, this signal is obviously ignored, because all the other active threads are blocking SIGTERM . 但是,显然会忽略此信号,因为所有其他活动线程都在阻塞SIGTERM Thus, I need to unblock signals in all other threads, before returning from the function that my cleanup thread is running. 因此,在从该函数返回我正在运行的清理线程之前,我需要取消所有其他线程中的信号。 Is there a function call that can be used to set pthread_sigmask on other threads? 是否有一个函数调用可用于在其他线程上设置pthread_sigmask

There is no call that can be used to set signal mask on other threads. 没有可用于在其他线程上设置信号掩码的调用。 Received signal is a trigger and no sense to resend that signal for itself. 接收到的信号是触发信号,没有意义重新发送该信号。 Try to use pthreads condition variables instead. 尝试改用pthreads 条件变量

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM