35.2.2.2 Controlling the Initial Signal Mask of a New Thread

The GNU C Library provides a way to specify the initial signal mask of a thread created using pthread_create, passing a thread attribute object configured for this purpose.

Function: int pthread_attr_setsigmask_np (pthread_attr_t *attr, const sigset_t *sigmask)

Preliminary: | MT-Safe | AS-Unsafe heap | AC-Unsafe mem | See POSIX Safety Concepts.

Change the initial signal mask specified by attr. If sigmask is not NULL, the initial signal mask for new threads created with attr is set to *sigmask. If sigmask is NULL, attr will no longer specify an explicit signal mask, so that the initial signal mask of the new thread is inherited from the thread that calls pthread_create.

This function returns zero on success, and ENOMEM on memory allocation failure.

Function: int pthread_attr_getsigmask_np (const pthread_attr_t *attr, sigset_t *sigmask)

Preliminary: | MT-Safe | AS-Unsafe heap | AC-Unsafe mem | See POSIX Safety Concepts.

Retrieve the signal mask stored in attr and copy it to *sigmask. If the signal mask has not been set, return the special constant PTHREAD_ATTR_NO_SIGMASK_NP, otherwise return zero.

Obtaining the signal mask only works if it has been previously stored by pthread_attr_setsigmask_np. For example, the pthread_getattr_np function does not obtain the current signal mask of the specified thread, and pthread_attr_getsigmask_np will subsequently report the signal mask as unset.

Macro: int PTHREAD_ATTR_NO_SIGMASK_NP

The special value returned by pthread_attr_setsigmask_np to indicate that no signal mask has been set for the attribute.

It is possible to create a new thread with a specific signal mask without using these functions. On the thread that calls pthread_create, the required steps for the general case are:

  1. Mask all signals, and save the old signal mask, using pthread_sigmask. This ensures that the new thread will be created with all signals masked, so that no signals can be delivered to the thread until the desired signal mask is set.
  2. Call pthread_create to create the new thread, passing the desired signal mask to the thread start routine (which could be a wrapper function for the actual thread start routine). It may be necessary to make a copy of the desired signal mask on the heap, so that the life-time of the copy extends to the point when the start routine needs to access the signal mask.
  3. Restore the thread’s signal mask, to the set that was saved in the first step.

The start routine for the created thread needs to locate the desired signal mask and use pthread_sigmask to apply it to the thread. If the signal mask was copied to a heap allocation, the copy should be freed.