]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man3/pthread_cancel.3
man*/: srcfix (Use .P instead of .PP or .LP)
[thirdparty/man-pages.git] / man3 / pthread_cancel.3
1 '\" t
2 .\" Copyright (c) 2008 Linux Foundation, written by Michael Kerrisk
3 .\" <mtk.manpages@gmail.com>
4 .\"
5 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
6 .\"
7 .TH pthread_cancel 3 (date) "Linux man-pages (unreleased)"
8 .SH NAME
9 pthread_cancel \- send a cancelation request to a thread
10 .SH LIBRARY
11 POSIX threads library
12 .RI ( libpthread ", " \-lpthread )
13 .SH SYNOPSIS
14 .nf
15 .B #include <pthread.h>
16 .P
17 .BI "int pthread_cancel(pthread_t " thread );
18 .fi
19 .SH DESCRIPTION
20 The
21 .BR pthread_cancel ()
22 function sends a cancelation request to the thread
23 .IR thread .
24 Whether and when the target thread
25 reacts to the cancelation request depends on
26 two attributes that are under the control of that thread:
27 its cancelability
28 .I state
29 and
30 .IR type .
31 .P
32 A thread's cancelability state, determined by
33 .BR pthread_setcancelstate (3),
34 can be
35 .I enabled
36 (the default for new threads) or
37 .IR disabled .
38 If a thread has disabled cancelation,
39 then a cancelation request remains queued until the thread
40 enables cancelation.
41 If a thread has enabled cancelation,
42 then its cancelability type determines when cancelation occurs.
43 .P
44 A thread's cancelation type, determined by
45 .BR pthread_setcanceltype (3),
46 may be either
47 .I asynchronous
48 or
49 .I deferred
50 (the default for new threads).
51 Asynchronous cancelability
52 means that the thread can be canceled at any time
53 (usually immediately, but the system does not guarantee this).
54 Deferred cancelability means that cancelation will be delayed until
55 the thread next calls a function that is a
56 .IR "cancelation point" .
57 A list of functions that are or may be cancelation points is provided in
58 .BR pthreads (7).
59 .P
60 When a cancelation requested is acted on, the following steps occur for
61 .I thread
62 (in this order):
63 .IP (1) 5
64 Cancelation clean-up handlers are popped
65 (in the reverse of the order in which they were pushed) and called.
66 (See
67 .BR pthread_cleanup_push (3).)
68 .IP (2)
69 Thread-specific data destructors are called,
70 in an unspecified order.
71 (See
72 .BR pthread_key_create (3).)
73 .IP (3)
74 The thread is terminated.
75 (See
76 .BR pthread_exit (3).)
77 .P
78 The above steps happen asynchronously with respect to the
79 .BR pthread_cancel ()
80 call;
81 the return status of
82 .BR pthread_cancel ()
83 merely informs the caller whether the cancelation request
84 was successfully queued.
85 .P
86 After a canceled thread has terminated,
87 a join with that thread using
88 .BR pthread_join (3)
89 obtains
90 .B PTHREAD_CANCELED
91 as the thread's exit status.
92 (Joining with a thread is the only way to know that cancelation
93 has completed.)
94 .SH RETURN VALUE
95 On success,
96 .BR pthread_cancel ()
97 returns 0;
98 on error, it returns a nonzero error number.
99 .SH ERRORS
100 .TP
101 .B ESRCH
102 No thread with the ID
103 .I thread
104 could be found.
105 .SH ATTRIBUTES
106 For an explanation of the terms used in this section, see
107 .BR attributes (7).
108 .TS
109 allbox;
110 lbx lb lb
111 l l l.
112 Interface Attribute Value
113 T{
114 .na
115 .nh
116 .BR pthread_cancel ()
117 T} Thread safety MT-Safe
118 .TE
119 .SH VERSIONS
120 On Linux, cancelation is implemented using signals.
121 Under the NPTL threading implementation,
122 the first real-time signal (i.e., signal 32) is used for this purpose.
123 On LinuxThreads, the second real-time signal is used,
124 if real-time signals are available, otherwise
125 .B SIGUSR2
126 is used.
127 .SH STANDARDS
128 POSIX.1-2008.
129 .SH HISTORY
130 glibc 2.0
131 POSIX.1-2001.
132 .SH EXAMPLES
133 The program below creates a thread and then cancels it.
134 The main thread joins with the canceled thread to check
135 that its exit status was
136 .BR PTHREAD_CANCELED .
137 The following shell session shows what happens when we run the program:
138 .P
139 .in +4n
140 .EX
141 $ ./a.out
142 thread_func(): started; cancelation disabled
143 main(): sending cancelation request
144 thread_func(): about to enable cancelation
145 main(): thread was canceled
146 .EE
147 .in
148 .SS Program source
149 \&
150 .\" SRC BEGIN (pthread_cancel.c)
151 .EX
152 #include <errno.h>
153 #include <pthread.h>
154 #include <stdio.h>
155 #include <stdlib.h>
156 #include <unistd.h>
157 \&
158 #define handle_error_en(en, msg) \e
159 do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
160 \&
161 static void *
162 thread_func(void *ignored_argument)
163 {
164 int s;
165 \&
166 /* Disable cancelation for a while, so that we don\[aq]t
167 immediately react to a cancelation request. */
168 \&
169 s = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
170 if (s != 0)
171 handle_error_en(s, "pthread_setcancelstate");
172 \&
173 printf("%s(): started; cancelation disabled\en", __func__);
174 sleep(5);
175 printf("%s(): about to enable cancelation\en", __func__);
176 \&
177 s = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
178 if (s != 0)
179 handle_error_en(s, "pthread_setcancelstate");
180 \&
181 /* sleep() is a cancelation point. */
182 \&
183 sleep(1000); /* Should get canceled while we sleep */
184 \&
185 /* Should never get here. */
186 \&
187 printf("%s(): not canceled!\en", __func__);
188 return NULL;
189 }
190 \&
191 int
192 main(void)
193 {
194 pthread_t thr;
195 void *res;
196 int s;
197 \&
198 /* Start a thread and then send it a cancelation request. */
199 \&
200 s = pthread_create(&thr, NULL, &thread_func, NULL);
201 if (s != 0)
202 handle_error_en(s, "pthread_create");
203 \&
204 sleep(2); /* Give thread a chance to get started */
205 \&
206 printf("%s(): sending cancelation request\en", __func__);
207 s = pthread_cancel(thr);
208 if (s != 0)
209 handle_error_en(s, "pthread_cancel");
210 \&
211 /* Join with thread to see what its exit status was. */
212 \&
213 s = pthread_join(thr, &res);
214 if (s != 0)
215 handle_error_en(s, "pthread_join");
216 \&
217 if (res == PTHREAD_CANCELED)
218 printf("%s(): thread was canceled\en", __func__);
219 else
220 printf("%s(): thread wasn\[aq]t canceled (shouldn\[aq]t happen!)\en",
221 __func__);
222 exit(EXIT_SUCCESS);
223 }
224 .EE
225 .\" SRC END
226 .SH SEE ALSO
227 .ad l
228 .nh
229 .BR pthread_cleanup_push (3),
230 .BR pthread_create (3),
231 .BR pthread_exit (3),
232 .BR pthread_join (3),
233 .BR pthread_key_create (3),
234 .BR pthread_setcancelstate (3),
235 .BR pthread_setcanceltype (3),
236 .BR pthread_testcancel (3),
237 .BR pthreads (7)