cmultithreadingpthreadssigprocmask

Relation of sigprocmask and pthread_sigmask for multithreaded program


sigprocmask(2) says:

Each of the threads in a process has its own signal mask.

, but also:

The use of sigprocmask() is unspecified in a multithreaded process; see pthread_sigmask(3).

pthread_sigmask(3) states, that:

The pthread_sigmask() function is just like sigprocmask(2), with the difference that its use in multithreaded programs is explicitly specified by POSIX.1. Other differences are noted in this page.

, but than there are no notes about that.

However https://docs.oracle.com/cd/E19120-01/open.solaris/816-5137/gen-19/index.html says, that:

The call to sigprocmask() in a multithreaded process is equivalent to a call to pthread_sigmask().

and calling sigprocmask also works.

So it is safe to call sigprocmask? Is it intended?


Solution

  • So it is safe to call sigprocmask? Is it intended?

    It is not portable for a multithreaded process to call sigprocmask(), in the sense that the POSIX standard says:

    The sigprocmask() function shall be equivalent to pthread_sigmask(), except that its behavior is unspecified if called from a multi-threaded process, and on error it returns -1 and sets errno to the error number instead of returning the error number directly.

    The bit about the return value is probably what the manual page you were looking at was talking about as "noted in this page". That does not necessarily mean "presented in a Notes section".

    If Solaris is the only execution environment you care about then its docs do seem to say that you can use sigprocmask() even in multithreaded programs (but don't overlook the difference in return value semantics). On the other hand, if you want to provide for broad portability by relying only on POSIX, then you must not assume that it is safe for a multithreaded program to call sigprocmask().

    On the third hand, in a POSIX environment, any program can call pthread_sigmask() and expect the documented behavior, even if it does not otherwise use pthreads. (Every process has at least one thread, that being the one that enters main()).

    and calling sigprocmask also works.

    It is extremely fraught to try to evaluate whether C code has well defined behavior by running it to see what happens. Even if the observable behavior appears to be what you want, you cannot be confident that there isn't any other, unwanted behavior, or that you might not see different behavior under other circumstances. Such experiments do not inspire confidence in me.