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 2021-03-22 "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
134 Interface Attribute Value
136 .BR pthread_cancel ()
137 T} Thread safety MT-Safe
143 POSIX.1-2001, POSIX.1-2008.
145 On Linux, cancellation is implemented using signals.
146 Under the NPTL threading implementation,
147 the first real-time signal (i.e., signal 32) is used for this purpose.
148 On LinuxThreads, the second real-time signal is used,
149 if real-time signals are available, otherwise
153 The program below creates a thread and then cancels it.
154 The main thread joins with the canceled thread to check
155 that its exit status was
156 .BR PTHREAD_CANCELED .
157 The following shell session shows what happens when we run the program:
162 thread_func(): started; cancellation disabled
163 main(): sending cancellation request
164 thread_func(): about to enable cancellation
165 main(): thread was canceled
177 #define handle_error_en(en, msg) \e
178 do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
181 thread_func(void *ignored_argument)
185 /* Disable cancellation for a while, so that we don\(aqt
186 immediately react to a cancellation request. */
188 s = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
190 handle_error_en(s, "pthread_setcancelstate");
192 printf("thread_func(): started; cancellation disabled\en");
194 printf("thread_func(): about to enable cancellation\en");
196 s = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
198 handle_error_en(s, "pthread_setcancelstate");
200 /* sleep() is a cancellation point. */
202 sleep(1000); /* Should get canceled while we sleep */
204 /* Should never get here. */
206 printf("thread_func(): not canceled!\en");
217 /* Start a thread and then send it a cancellation request. */
219 s = pthread_create(&thr, NULL, &thread_func, NULL);
221 handle_error_en(s, "pthread_create");
223 sleep(2); /* Give thread a chance to get started */
225 printf("main(): sending cancellation request\en");
226 s = pthread_cancel(thr);
228 handle_error_en(s, "pthread_cancel");
230 /* Join with thread to see what its exit status was. */
232 s = pthread_join(thr, &res);
234 handle_error_en(s, "pthread_join");
236 if (res == PTHREAD_CANCELED)
237 printf("main(): thread was canceled\en");
239 printf("main(): thread wasn\(aqt canceled (shouldn\(aqt happen!)\en");
246 .BR pthread_cleanup_push (3),
247 .BR pthread_create (3),
248 .BR pthread_exit (3),
249 .BR pthread_join (3),
250 .BR pthread_key_create (3),
251 .BR pthread_setcancelstate (3),
252 .BR pthread_setcanceltype (3),
253 .BR pthread_testcancel (3),