1PTHREAD_SIGMASK(3) Linux Programmer's Manual PTHREAD_SIGMASK(3)
2
3
4
6 pthread_sigmask - examine and change mask of blocked signals
7
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
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
29 On success, pthread_sigmask() returns 0; on error, it returns an error
30 number.
31
33 See sigprocmask(2).
34
36 For an explanation of the terms used in this section, see at‐
37 tributes(7).
38
39 ┌────────────────────────────────────────────┬───────────────┬─────────┐
40 │Interface │ Attribute │ Value │
41 ├────────────────────────────────────────────┼───────────────┼─────────┤
42 │pthread_sigmask() │ Thread safety │ MT-Safe │
43 └────────────────────────────────────────────┴───────────────┴─────────┘
44
46 POSIX.1-2001, POSIX.1-2008.
47
49 A new thread inherits a copy of its creator's signal mask.
50
51 The glibc pthread_sigmask() function silently ignores attempts to block
52 the two real-time signals that are used internally by the NPTL thread‐
53 ing implementation. See nptl(7) for details.
54
56 The program below blocks some signals in the main thread, and then cre‐
57 ates a dedicated thread to fetch those signals via sigwait(3). The
58 following shell session demonstrates its use:
59
60 $ ./a.out &
61 [1] 5423
62 $ kill -QUIT %1
63 Signal handling thread got signal 3
64 $ kill -USR1 %1
65 Signal handling thread got signal 10
66 $ kill -TERM %1
67 [1]+ Terminated ./a.out
68
69 Program source
70
71 #include <pthread.h>
72 #include <stdio.h>
73 #include <stdlib.h>
74 #include <unistd.h>
75 #include <signal.h>
76 #include <errno.h>
77
78 /* Simple error handling functions */
79
80 #define handle_error_en(en, msg) \
81 do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
82
83 static void *
84 sig_thread(void *arg)
85 {
86 sigset_t *set = arg;
87 int s, sig;
88
89 for (;;) {
90 s = sigwait(set, &sig);
91 if (s != 0)
92 handle_error_en(s, "sigwait");
93 printf("Signal handling thread got signal %d\n", sig);
94 }
95 }
96
97 int
98 main(int argc, char *argv[])
99 {
100 pthread_t thread;
101 sigset_t set;
102 int s;
103
104 /* Block SIGQUIT and SIGUSR1; other threads created by main()
105 will inherit a copy of the signal mask. */
106
107 sigemptyset(&set);
108 sigaddset(&set, SIGQUIT);
109 sigaddset(&set, SIGUSR1);
110 s = pthread_sigmask(SIG_BLOCK, &set, NULL);
111 if (s != 0)
112 handle_error_en(s, "pthread_sigmask");
113
114 s = pthread_create(&thread, NULL, &sig_thread, &set);
115 if (s != 0)
116 handle_error_en(s, "pthread_create");
117
118 /* Main thread carries on to create other threads and/or do
119 other work. */
120
121 pause(); /* Dummy pause so we can test program */
122 }
123
125 sigaction(2), sigpending(2), sigprocmask(2), pthread_attr_setsig‐
126 mask_np(3), pthread_create(3), pthread_kill(3), sigsetops(3),
127 pthreads(7), signal(7)
128
130 This page is part of release 5.12 of the Linux man-pages project. A
131 description of the project, information about reporting bugs, and the
132 latest version of this page, can be found at
133 https://www.kernel.org/doc/man-pages/.
134
135
136
137Linux 2021-03-22 PTHREAD_SIGMASK(3)