1

http://pubs.opengroup.org/onlinepubs/009604499/functions/sigprocmask.html

文档显示了需要存储在全局范围变量中的所有信号的示例(在信号线程启动之前wait在 main() 填充。在示例信号线程在阻塞信号之后启动。 SIG_BLOCK

它是否正确并且不会导致一些未定义的行为或崩溃,如果我将在 main() 阻塞信号之前 SIG_BLOCK创建我的信号线程,然后我将阻塞所有其他新创建的线程的信号?

4

1 回答 1

1

只要信号线程明确阻止它最终将等待的信号sigwait,那么它就是安全的。

例如,以下两个流程示例都是安全的:

main:
  create_signal_thread() // Inherits mask from main, so signals are not blocked.
  block_signals()        
  create_more_threads()  // Inherit mask from main, so signals are blocked.

signal_thread:
  block_signals()        // Must block signals, as this thread will wait on them.
  sigwait()              // Behavior is defined.

main:
  block_signals()        
  create_signal_thread() // Inherits mask from main, so signals are blocked.
  create_more_threads()  // Inherit mask from main, so signals are blocked.

signal_thread:
  sigwait()              // Behavior is defined.

然而,这个例子并不安全:

main:
  create_signal_thread() // Inherits mask from main, so signals are not blocked.
  block_signals()        
  create_more_threads()  // Inherit mask from main, so signals are blocked.

signal_thread:
  sigwait()              // Behavior is undefined.

以下是相关摘录sigwait

set 定义的信号将在调用时被阻塞,sigwait();否则行为未定义。sigwait()未指定集合中信号对信号动作的影响。

于 2012-06-21T13:36:35.673 回答