Using the sigwait call to handle asynchronous signals

In a multithreaded environment, you can use the sigwait UNIX system call to handle asynchronous signals synchronously. This helps your applications avoid interruptions and behave more predictably.

Typically, a dedicated signal-handler thread uses sigwait to wait for asynchronous signals. A signal mask provided for sigwait indicates which signals to wait for. When a signal is delivered, sigwait returns the signal number and the signal-handler thread executes the signal handler.

For a process to receive asynchronous signals by means of sigwait, you must:

  1. Create a dedicated signal-handler thread in which sigwait is invoked to capture the asynchronous signals. Mask these signals for this thread.

  2. For all other threads, mask the signals specified in the set parameter to sigwait.

    When you mask asynchronous signals in all threads, you make sure that signals are held while a signal-handler thread executes the previous signal’s handler. In subsequent calls to sigwait, the blocked signals are returned.

    If you mask all asynchronous signals at the start of the program, all threads spawned by the main thread inherit this signal mask.