]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/pthread_sigmask.3
Many pages: LIBRARY: srcfix
[thirdparty/man-pages.git] / man3 / pthread_sigmask.3
CommitLineData
b4ed7957
MK
1.\" Copyright (c) 2009 Linux Foundation, written by Michael Kerrisk
2.\" <mtk.manpages@gmail.com>
3.\"
5fbde956 4.\" SPDX-License-Identifier: Linux-man-pages-copyleft
b4ed7957 5.\"
1d767b55 6.TH PTHREAD_SIGMASK 3 2021-03-22 "Linux" "Linux Programmer's Manual"
b4ed7957
MK
7.SH NAME
8pthread_sigmask \- examine and change mask of blocked signals
0a9455e8
AC
9.SH LIBRARY
10POSIX threads library
8fc3b2cf 11.RI ( libpthread ", " \-lpthread )
b4ed7957
MK
12.SH SYNOPSIS
13.nf
14.B #include <signal.h>
f90f031e 15.PP
b4ed7957
MK
16.BI "int pthread_sigmask(int " how ", const sigset_t *" set \
17", sigset_t *" oldset );
18.fi
68e4db0a 19.PP
d39ad78f 20.RS -4
fc1d1e69
MK
21Feature Test Macro Requirements for glibc (see
22.BR feature_test_macros (7)):
d39ad78f 23.RE
68e4db0a 24.PP
fc1d1e69 25.BR pthread_sigmask ():
9d2adbae 26.nf
5c10d2c5 27 _POSIX_C_SOURCE >= 199506L || _XOPEN_SOURCE >= 500
9d2adbae 28.fi
b4ed7957
MK
29.SH DESCRIPTION
30The
31.BR pthread_sigmask ()
32function is just like
33.BR sigprocmask (2),
34with the difference that its use in multithreaded programs
3196395a 35is explicitly specified by POSIX.1.
b4ed7957 36Other differences are noted in this page.
847e0d88 37.PP
b4ed7957
MK
38For a description of the arguments and operation of this function, see
39.BR sigprocmask (2).
40.SH RETURN VALUE
41On success,
42.BR pthread_sigmask ()
43returns 0;
44on error, it returns an error number.
45.SH ERRORS
46See
47.BR sigprocmask (2).
21f80449 48.SH ATTRIBUTES
f5104723
PH
49For an explanation of the terms used in this section, see
50.BR attributes (7).
c466875e
MK
51.ad l
52.nh
f5104723
PH
53.TS
54allbox;
c466875e 55lbx lb lb
f5104723
PH
56l l l.
57Interface Attribute Value
58T{
21f80449 59.BR pthread_sigmask ()
f5104723
PH
60T} Thread safety MT-Safe
61.TE
c466875e
MK
62.hy
63.ad
64.sp 1
b4ed7957 65.SH CONFORMING TO
3196395a 66POSIX.1-2001, POSIX.1-2008.
b4ed7957
MK
67.SH NOTES
68A new thread inherits a copy of its creator's signal mask.
847e0d88 69.PP
bce38798
MK
70The glibc
71.BR pthread_sigmask ()
72function silently ignores attempts to block the two real-time signals that
73are used internally by the NPTL threading implementation.
74See
75.BR nptl (7)
76for details.
a14af333 77.SH EXAMPLES
b4ed7957
MK
78The program below blocks some signals in the main thread,
79and then creates a dedicated thread to fetch those signals via
80.BR sigwait (3).
81The following shell session demonstrates its use:
847e0d88 82.PP
b4ed7957 83.in +4n
b8302363 84.EX
b4ed7957
MK
85.RB "$" " ./a.out &"
86[1] 5423
87.RB "$" " kill \-QUIT %1"
88Signal handling thread got signal 3
89.RB "$" " kill \-USR1 %1"
90Signal handling thread got signal 10
91.RB "$" " kill \-TERM %1"
92[1]+ Terminated ./a.out
b8302363 93.EE
b4ed7957
MK
94.in
95.SS Program source
96\&
e7d0bb47 97.EX
b4ed7957
MK
98#include <pthread.h>
99#include <stdio.h>
100#include <stdlib.h>
101#include <unistd.h>
102#include <signal.h>
103#include <errno.h>
104
105/* Simple error handling functions */
106
d1a71985 107#define handle_error_en(en, msg) \e
b4ed7957
MK
108 do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
109
110static void *
111sig_thread(void *arg)
112{
13f78d96 113 sigset_t *set = arg;
b4ed7957
MK
114 int s, sig;
115
116 for (;;) {
117 s = sigwait(set, &sig);
118 if (s != 0)
119 handle_error_en(s, "sigwait");
d1a71985 120 printf("Signal handling thread got signal %d\en", sig);
b4ed7957
MK
121 }
122}
123
124int
125main(int argc, char *argv[])
126{
127 pthread_t thread;
128 sigset_t set;
129 int s;
130
4a905b92
MK
131 /* Block SIGQUIT and SIGUSR1; other threads created by main()
132 will inherit a copy of the signal mask. */
b4ed7957
MK
133
134 sigemptyset(&set);
135 sigaddset(&set, SIGQUIT);
136 sigaddset(&set, SIGUSR1);
137 s = pthread_sigmask(SIG_BLOCK, &set, NULL);
138 if (s != 0)
139 handle_error_en(s, "pthread_sigmask");
140
a0303c05 141 s = pthread_create(&thread, NULL, &sig_thread, &set);
b4ed7957
MK
142 if (s != 0)
143 handle_error_en(s, "pthread_create");
144
145 /* Main thread carries on to create other threads and/or do
c6beb8a1 146 other work. */
b4ed7957
MK
147
148 pause(); /* Dummy pause so we can test program */
149}
e7d0bb47 150.EE
b4ed7957
MK
151.SH SEE ALSO
152.BR sigaction (2),
153.BR sigpending (2),
9e88dbc3 154.BR sigprocmask (2),
96841f0e 155.BR pthread_attr_setsigmask_np (3),
b4ed7957
MK
156.BR pthread_create (3),
157.BR pthread_kill (3),
158.BR sigsetops (3),
159.BR pthreads (7),
160.BR signal (7)