pthread_sigmask(3)

โœจ NAME โœจ

pthread_sigmask โ€” the glow-up version of blocking signals: examine and change your mask of blocked signals ๐Ÿ’…

Feels like a libpthread (POSIX threads library) moment, ngl โ€” link it with -lpthread. ๐Ÿงต


โœจ SYNOPSIS โœจ

#include <signal.h>

int pthread_sigmask(int how, const sigset_t *set, sigset_t *oldset);

๐ŸŽ€ Feature Test Macro Requirements for glibc

(go check feature_test_macros(7) bestie, it's all there)

pthread_sigmask():

_POSIX_C_SOURCE >= 199506L || _XOPEN_SOURCE >= 500

๐Ÿ’– DESCRIPTION ๐Ÿ’–

okay so pthread_sigmask() is literally just like sigprocmask(2), except โ€” and this is the part that eats โ€” its use in multithreaded programs is explicitly specified by POSIX.1. That's the whole plot twist. The other differences are noted further down this page, so don't skip them. ๐Ÿ™ˆ

For the full tea on the arguments and how this function actually operates, go read sigprocmask(2). No cap, everything's there.


๐ŸŒธ RETURN VALUE ๐ŸŒธ

Success? She returns 0. Error? She returns an error number. That's it, that's the vibe. โœจ


๐Ÿฆ‹ ERRORS ๐Ÿฆ‹

See sigprocmask(2). Yeah bestie, we're pointing you there again โ€” it's giving "read the other page." ๐Ÿ‘‘


๐Ÿ’œ ATTRIBUTES ๐Ÿ’œ

For an explanation of the terms used in this section, see attributes(7).

  • Interface: pthread_sigmask()
  • Attribute: Thread safety
  • Value: MT-Safe ๐Ÿ”ฅ

๐ŸŒˆ STANDARDS ๐ŸŒˆ

POSIX.1-2008. Periodt.


โญ HISTORY โญ

POSIX.1-2001. She's been iconic for a minute. ๐Ÿ‘‘


๐Ÿ“ NOTES ๐Ÿ“

A new thread inherits a copy of its creator's signal mask. Genetics, but make it signals. ๐Ÿงฌ

The glibc pthread_sigmask() function silently ignores attempts to block the two real-time signals that are used internally by the NPTL threading implementation. Silently!! She just pretends she didn't hear you. ๐Ÿ™ˆ See nptl(7) for the details.


๐Ÿงš EXAMPLES ๐Ÿงš

The program below blocks some signals in the main thread, and then creates a dedicated thread to fetch those signals via sigwait(3). Here's a shell session showing it in action, live and in 4K:

$ ./a.out &
[1] 5423
$ kill -QUIT %1
Signal handling thread got signal 3
$ kill -USR1 %1
Signal handling thread got signal 10
$ kill -TERM %1
[1]+  Terminated              ./a.out

๐ŸŒธ Program source ๐ŸŒธ

#include <err.h>
#include <errno.h>
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

/* Simple error handling functions */

static void *
sig_thread(void *arg)
{
    sigset_t *set = arg;
    int s, sig;

    for (;;) {
        s = sigwait(set, &sig);
        if (s != 0)
            errc(EXIT_FAILURE, s, "sigwait");
        printf("Signal handling thread got signal %d\n", sig);
    }
}

int
main(void)
{
    pthread_t thread;
    sigset_t set;
    int s;

    /* Block SIGQUIT and SIGUSR1; other threads created by main()
       will inherit a copy of the signal mask.  */

    sigemptyset(&set);
    sigaddset(&set, SIGQUIT);
    sigaddset(&set, SIGUSR1);
    s = pthread_sigmask(SIG_BLOCK, &set, NULL);
    if (s != 0)
        errc(EXIT_FAILURE, s, "pthread_sigmask");

    s = pthread_create(&thread, NULL, &sig_thread, &set);
    if (s != 0)
        errc(EXIT_FAILURE, s, "pthread_create");

    /* Main thread carries on to create other threads and/or do
       other work.  */

    pause();            /* Dummy pause so we can test program */
}

๐Ÿ‘‘ SEE ALSO ๐Ÿ‘‘

sigaction(2), sigpending(2), sigprocmask(2), pthread_attr_setsigmask_np(3), pthread_create(3), pthread_kill(3), sigsetops(3), pthreads(7), signal(7)


okay that's the whole page, you ate that ๐Ÿ’… this was Linux man-pages 6.20, dated 2025-09-21 โ€” yes, pthread_sigmask(3) said it, bestie. go forth and mask responsibly ๐Ÿซถ๐ŸŽ€

๐Ÿ“ฆ where this page comes from
  • arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
  • nixpkgs unstable: man-pages 6.20
  • ๐Ÿ“œ the page's license: Linux-man-pages-copyleft
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms