1pthread_sigmask(3) Library Functions Manual pthread_sigmask(3)
2
3
4
6 pthread_sigmask - examine and change mask of blocked signals
7
9 POSIX threads library (libpthread, -lpthread)
10
12 #include <signal.h>
13
14 int pthread_sigmask(int how, const sigset_t *set, sigset_t *oldset);
15
16 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
17
18 pthread_sigmask():
19 _POSIX_C_SOURCE >= 199506L || _XOPEN_SOURCE >= 500
20
22 The pthread_sigmask() function is just like sigprocmask(2), with the
23 difference that its use in multithreaded programs is explicitly speci‐
24 fied by POSIX.1. Other differences are noted in this page.
25
26 For a description of the arguments and operation of this function, see
27 sigprocmask(2).
28
30 On success, pthread_sigmask() returns 0; on error, it returns an error
31 number.
32
34 See sigprocmask(2).
35
37 For an explanation of the terms used in this section, see at‐
38 tributes(7).
39
40 ┌────────────────────────────────────────────┬───────────────┬─────────┐
41 │Interface │ Attribute │ Value │
42 ├────────────────────────────────────────────┼───────────────┼─────────┤
43 │pthread_sigmask() │ Thread safety │ MT-Safe │
44 └────────────────────────────────────────────┴───────────────┴─────────┘
45
47 POSIX.1-2008.
48
50 POSIX.1-2001.
51
53 A new thread inherits a copy of its creator's signal mask.
54
55 The glibc pthread_sigmask() function silently ignores attempts to block
56 the two real-time signals that are used internally by the NPTL
57 threading implementation. See nptl(7) for details.
58
60 The program below blocks some signals in the main thread, and then
61 creates a dedicated thread to fetch those signals via sigwait(3). The
62 following shell session demonstrates its use:
63
64 $ ./a.out &
65 [1] 5423
66 $ kill -QUIT %1
67 Signal handling thread got signal 3
68 $ kill -USR1 %1
69 Signal handling thread got signal 10
70 $ kill -TERM %1
71 [1]+ Terminated ./a.out
72
73 Program source
74
75 #include <errno.h>
76 #include <pthread.h>
77 #include <signal.h>
78 #include <stdio.h>
79 #include <stdlib.h>
80 #include <unistd.h>
81
82 /* Simple error handling functions */
83
84 #define handle_error_en(en, msg) \
85 do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
86
87 static void *
88 sig_thread(void *arg)
89 {
90 sigset_t *set = arg;
91 int s, sig;
92
93 for (;;) {
94 s = sigwait(set, &sig);
95 if (s != 0)
96 handle_error_en(s, "sigwait");
97 printf("Signal handling thread got signal %d\n", sig);
98 }
99 }
100
101 int
102 main(void)
103 {
104 pthread_t thread;
105 sigset_t set;
106 int s;
107
108 /* Block SIGQUIT and SIGUSR1; other threads created by main()
109 will inherit a copy of the signal mask. */
110
111 sigemptyset(&set);
112 sigaddset(&set, SIGQUIT);
113 sigaddset(&set, SIGUSR1);
114 s = pthread_sigmask(SIG_BLOCK, &set, NULL);
115 if (s != 0)
116 handle_error_en(s, "pthread_sigmask");
117
118 s = pthread_create(&thread, NULL, &sig_thread, &set);
119 if (s != 0)
120 handle_error_en(s, "pthread_create");
121
122 /* Main thread carries on to create other threads and/or do
123 other work. */
124
125 pause(); /* Dummy pause so we can test program */
126 }
127
129 sigaction(2), sigpending(2), sigprocmask(2), pthread_attr_setsig‐
130 mask_np(3), pthread_create(3), pthread_kill(3), sigsetops(3),
131 pthreads(7), signal(7)
132
133
134
135Linux man-pages 6.05 2023-07-20 pthread_sigmask(3)