1 .\" Copyright (c) 2008 Linux Foundation, written by Michael Kerrisk
2 .\" <mtk.manpages@gmail.com>
4 .\" %%%LICENSE_START(VERBATIM)
5 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
9 .\" Permission is granted to copy and distribute modified versions of this
10 .\" manual under the conditions for verbatim copying, provided that the
11 .\" entire resulting derived work is distributed under the terms of a
12 .\" permission notice identical to this one.
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date. The author(s) assume no
16 .\" responsibility for errors or omissions, or for damages resulting from
17 .\" the use of the information contained herein. The author(s) may not
18 .\" have taken the same level of care in the production of this manual,
19 .\" which is licensed free of charge, as they might when working
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
26 .TH PTHREAD_CANCEL 3 2019-03-06 "Linux" "Linux Programmer's Manual"
28 pthread_cancel \- send a cancellation request to a thread
31 .B #include <pthread.h>
33 .BI "int pthread_cancel(pthread_t " thread );
35 Compile and link with \fI\-pthread\fP.
40 function sends a cancellation request to the thread
42 Whether and when the target thread
43 reacts to the cancellation request depends on
44 two attributes that are under the control of that thread:
50 A thread's cancelability state, determined by
51 .BR pthread_setcancelstate (3),
54 (the default for new threads) or
56 If a thread has disabled cancellation,
57 then a cancellation request remains queued until the thread
59 If a thread has enabled cancellation,
60 then its cancelability type determines when cancellation occurs.
62 A thread's cancellation type, determined by
63 .BR pthread_setcanceltype (3),
68 (the default for new threads).
69 Asynchronous cancelability
70 means that the thread can be canceled at any time
71 (usually immediately, but the system does not guarantee this).
72 Deferred cancelability means that cancellation will be delayed until
73 the thread next calls a function that is a
74 .IR "cancellation point" .
75 A list of functions that are or may be cancellation points is provided in
78 When a cancellation requested is acted on, the following steps occur for
82 Cancellation clean-up handlers are popped
83 (in the reverse of the order in which they were pushed) and called.
85 .BR pthread_cleanup_push (3).)
87 Thread-specific data destructors are called,
88 in an unspecified order.
90 .BR pthread_key_create (3).)
92 The thread is terminated.
94 .BR pthread_exit (3).)
96 The above steps happen asynchronously with respect to the
100 .BR pthread_cancel ()
101 merely informs the caller whether the cancellation request
102 was successfully queued.
104 After a canceled thread has terminated,
105 a join with that thread using
109 as the thread's exit status.
110 (Joining with a thread is the only way to know that cancellation
114 .BR pthread_cancel ()
116 on error, it returns a nonzero error number.
120 No thread with the ID
124 .\" Available since glibc 2.0
126 For an explanation of the terms used in this section, see
132 Interface Attribute Value
134 .BR pthread_cancel ()
135 T} Thread safety MT-Safe
139 POSIX.1-2001, POSIX.1-2008.
141 On Linux, cancellation is implemented using signals.
142 Under the NPTL threading implementation,
143 the first real-time signal (i.e., signal 32) is used for this purpose.
144 On LinuxThreads, the second real-time signal is used,
145 if real-time signals are available, otherwise
149 The program below creates a thread and then cancels it.
150 The main thread joins with the canceled thread to check
151 that its exit status was
152 .BR PTHREAD_CANCELED .
153 The following shell session shows what happens when we run the program:
158 thread_func(): started; cancellation disabled
159 main(): sending cancellation request
160 thread_func(): about to enable cancellation
161 main(): thread was canceled
173 #define handle_error_en(en, msg) \e
174 do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
177 thread_func(void *ignored_argument)
181 /* Disable cancellation for a while, so that we don\(aqt
182 immediately react to a cancellation request */
184 s = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
186 handle_error_en(s, "pthread_setcancelstate");
188 printf("thread_func(): started; cancellation disabled\en");
190 printf("thread_func(): about to enable cancellation\en");
192 s = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
194 handle_error_en(s, "pthread_setcancelstate");
196 /* sleep() is a cancellation point */
198 sleep(1000); /* Should get canceled while we sleep */
200 /* Should never get here */
202 printf("thread_func(): not canceled!\en");
213 /* Start a thread and then send it a cancellation request */
215 s = pthread_create(&thr, NULL, &thread_func, NULL);
217 handle_error_en(s, "pthread_create");
219 sleep(2); /* Give thread a chance to get started */
221 printf("main(): sending cancellation request\en");
222 s = pthread_cancel(thr);
224 handle_error_en(s, "pthread_cancel");
226 /* Join with thread to see what its exit status was */
228 s = pthread_join(thr, &res);
230 handle_error_en(s, "pthread_join");
232 if (res == PTHREAD_CANCELED)
233 printf("main(): thread was canceled\en");
235 printf("main(): thread wasn\(aqt canceled (shouldn\(aqt happen!)\en");
242 .BR pthread_cleanup_push (3),
243 .BR pthread_create (3),
244 .BR pthread_exit (3),
245 .BR pthread_join (3),
246 .BR pthread_key_create (3),
247 .BR pthread_setcancelstate (3),
248 .BR pthread_setcanceltype (3),
249 .BR pthread_testcancel (3),