1PTHREAD_SIGMASK(3)         Linux Programmer's Manual        PTHREAD_SIGMASK(3)
2
3
4

NAME

6       pthread_sigmask - examine and change mask of blocked signals
7

SYNOPSIS

9       #include <signal.h>
10
11       int pthread_sigmask(int how, const sigset_t *set, sigset_t *oldset);
12
13       Compile and link with -pthread.
14
15   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
16
17       pthread_sigmask():
18           _POSIX_C_SOURCE >= 199506L || _XOPEN_SOURCE >= 500
19

DESCRIPTION

21       The  pthread_sigmask()  function  is just like sigprocmask(2), with the
22       difference that its use in multithreaded programs is explicitly  speci‐
23       fied by POSIX.1.  Other differences are noted in this page.
24
25       For  a description of the arguments and operation of this function, see
26       sigprocmask(2).
27

RETURN VALUE

29       On success, pthread_sigmask() returns 0; on error, it returns an  error
30       number.
31

ERRORS

33       See sigprocmask(2).
34

ATTRIBUTES

36       For   an   explanation   of   the  terms  used  in  this  section,  see
37       attributes(7).
38
39       ┌──────────────────┬───────────────┬─────────┐
40Interface         Attribute     Value   
41       ├──────────────────┼───────────────┼─────────┤
42pthread_sigmask() │ Thread safety │ MT-Safe │
43       └──────────────────┴───────────────┴─────────┘

CONFORMING TO

45       POSIX.1-2001, POSIX.1-2008.
46

NOTES

48       A new thread inherits a copy of its creator's signal mask.
49
50       The glibc pthread_sigmask() function silently ignores attempts to block
51       the  two real-time signals that are used internally by the NPTL thread‐
52       ing implementation.  See nptl(7) for details.
53

EXAMPLE

55       The program below blocks some signals in the main thread, and then cre‐
56       ates  a  dedicated  thread  to fetch those signals via sigwait(3).  The
57       following shell session demonstrates its use:
58
59           $ ./a.out &
60           [1] 5423
61           $ kill -QUIT %1
62           Signal handling thread got signal 3
63           $ kill -USR1 %1
64           Signal handling thread got signal 10
65           $ kill -TERM %1
66           [1]+  Terminated              ./a.out
67
68   Program source
69
70       #include <pthread.h>
71       #include <stdio.h>
72       #include <stdlib.h>
73       #include <unistd.h>
74       #include <signal.h>
75       #include <errno.h>
76
77       /* Simple error handling functions */
78
79       #define handle_error_en(en, msg) \
80               do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
81
82       static void *
83       sig_thread(void *arg)
84       {
85           sigset_t *set = arg;
86           int s, sig;
87
88           for (;;) {
89               s = sigwait(set, &sig);
90               if (s != 0)
91                   handle_error_en(s, "sigwait");
92               printf("Signal handling thread got signal %d\n", sig);
93           }
94       }
95
96       int
97       main(int argc, char *argv[])
98       {
99           pthread_t thread;
100           sigset_t set;
101           int s;
102
103           /* Block SIGQUIT and SIGUSR1; other threads created by main()
104              will inherit a copy of the signal mask. */
105
106           sigemptyset(&set);
107           sigaddset(&set, SIGQUIT);
108           sigaddset(&set, SIGUSR1);
109           s = pthread_sigmask(SIG_BLOCK, &set, NULL);
110           if (s != 0)
111               handle_error_en(s, "pthread_sigmask");
112
113           s = pthread_create(&thread, NULL, &sig_thread, (void *) &set);
114           if (s != 0)
115               handle_error_en(s, "pthread_create");
116
117           /* Main thread carries on to create other threads and/or do
118              other work */
119
120           pause();            /* Dummy pause so we can test program */
121       }
122

SEE ALSO

124       sigaction(2),   sigpending(2),    sigprocmask(2),    pthread_create(3),
125       pthread_kill(3), sigsetops(3), pthreads(7), signal(7)
126

COLOPHON

128       This  page  is  part of release 5.02 of the Linux man-pages project.  A
129       description of the project, information about reporting bugs,  and  the
130       latest     version     of     this    page,    can    be    found    at
131       https://www.kernel.org/doc/man-pages/.
132
133
134
135Linux                             2019-03-06                PTHREAD_SIGMASK(3)
Impressum