1 .\" Copyright (c) 2008 Linux Foundation, written by Michael Kerrisk
2 .\" <mtk.manpages@gmail.com>
4 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
6 .TH PTHREAD_CANCEL 3 2021-03-22 "Linux" "Linux Programmer's Manual"
8 pthread_cancel \- send a cancelation request to a thread
11 .RI ( libpthread ", " \-lpthread )
14 .B #include <pthread.h>
16 .BI "int pthread_cancel(pthread_t " thread );
21 function sends a cancelation request to the thread
23 Whether and when the target thread
24 reacts to the cancelation request depends on
25 two attributes that are under the control of that thread:
31 A thread's cancelability state, determined by
32 .BR pthread_setcancelstate (3),
35 (the default for new threads) or
37 If a thread has disabled cancelation,
38 then a cancelation request remains queued until the thread
40 If a thread has enabled cancelation,
41 then its cancelability type determines when cancelation occurs.
43 A thread's cancelation type, determined by
44 .BR pthread_setcanceltype (3),
49 (the default for new threads).
50 Asynchronous cancelability
51 means that the thread can be canceled at any time
52 (usually immediately, but the system does not guarantee this).
53 Deferred cancelability means that cancelation will be delayed until
54 the thread next calls a function that is a
55 .IR "cancelation point" .
56 A list of functions that are or may be cancelation points is provided in
59 When a cancelation requested is acted on, the following steps occur for
63 Cancellation clean-up handlers are popped
64 (in the reverse of the order in which they were pushed) and called.
66 .BR pthread_cleanup_push (3).)
68 Thread-specific data destructors are called,
69 in an unspecified order.
71 .BR pthread_key_create (3).)
73 The thread is terminated.
75 .BR pthread_exit (3).)
77 The above steps happen asynchronously with respect to the
82 merely informs the caller whether the cancelation request
83 was successfully queued.
85 After a canceled thread has terminated,
86 a join with that thread using
90 as the thread's exit status.
91 (Joining with a thread is the only way to know that cancelation
97 on error, it returns a nonzero error number.
101 No thread with the ID
105 .\" Available since glibc 2.0
107 For an explanation of the terms used in this section, see
115 Interface Attribute Value
117 .BR pthread_cancel ()
118 T} Thread safety MT-Safe
124 POSIX.1-2001, POSIX.1-2008.
126 On Linux, cancelation is implemented using signals.
127 Under the NPTL threading implementation,
128 the first real-time signal (i.e., signal 32) is used for this purpose.
129 On LinuxThreads, the second real-time signal is used,
130 if real-time signals are available, otherwise
134 The program below creates a thread and then cancels it.
135 The main thread joins with the canceled thread to check
136 that its exit status was
137 .BR PTHREAD_CANCELED .
138 The following shell session shows what happens when we run the program:
143 thread_func(): started; cancelation disabled
144 main(): sending cancelation request
145 thread_func(): about to enable cancelation
146 main(): thread was canceled
158 #define handle_error_en(en, msg) \e
159 do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
162 thread_func(void *ignored_argument)
166 /* Disable cancelation for a while, so that we don\(aqt
167 immediately react to a cancelation request. */
169 s = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
171 handle_error_en(s, "pthread_setcancelstate");
173 printf("thread_func(): started; cancelation disabled\en");
175 printf("thread_func(): about to enable cancelation\en");
177 s = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
179 handle_error_en(s, "pthread_setcancelstate");
181 /* sleep() is a cancelation point. */
183 sleep(1000); /* Should get canceled while we sleep */
185 /* Should never get here. */
187 printf("thread_func(): not canceled!\en");
198 /* Start a thread and then send it a cancelation request. */
200 s = pthread_create(&thr, NULL, &thread_func, NULL);
202 handle_error_en(s, "pthread_create");
204 sleep(2); /* Give thread a chance to get started */
206 printf("main(): sending cancelation request\en");
207 s = pthread_cancel(thr);
209 handle_error_en(s, "pthread_cancel");
211 /* Join with thread to see what its exit status was. */
213 s = pthread_join(thr, &res);
215 handle_error_en(s, "pthread_join");
217 if (res == PTHREAD_CANCELED)
218 printf("main(): thread was canceled\en");
220 printf("main(): thread wasn\(aqt canceled (shouldn\(aqt happen!)\en");
227 .BR pthread_cleanup_push (3),
228 .BR pthread_create (3),
229 .BR pthread_exit (3),
230 .BR pthread_join (3),
231 .BR pthread_key_create (3),
232 .BR pthread_setcancelstate (3),
233 .BR pthread_setcanceltype (3),
234 .BR pthread_testcancel (3),