2 .\" Copyright (c) 2008 Linux Foundation, written by Michael Kerrisk
3 .\" <mtk.manpages@gmail.com>
5 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
7 .TH pthread_cancel 3 (date) "Linux man-pages (unreleased)"
9 pthread_cancel \- send a cancelation request to a thread
12 .RI ( libpthread ", " \-lpthread )
15 .B #include <pthread.h>
17 .BI "int pthread_cancel(pthread_t " thread );
22 function sends a cancelation request to the 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:
32 A thread's cancelability state, determined by
33 .BR pthread_setcancelstate (3),
36 (the default for new threads) or
38 If a thread has disabled cancelation,
39 then a cancelation request remains queued until the thread
41 If a thread has enabled cancelation,
42 then its cancelability type determines when cancelation occurs.
44 A thread's cancelation type, determined by
45 .BR pthread_setcanceltype (3),
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
60 When a cancelation requested is acted on, the following steps occur for
64 Cancelation clean-up handlers are popped
65 (in the reverse of the order in which they were pushed) and called.
67 .BR pthread_cleanup_push (3).)
69 Thread-specific data destructors are called,
70 in an unspecified order.
72 .BR pthread_key_create (3).)
74 The thread is terminated.
76 .BR pthread_exit (3).)
78 The above steps happen asynchronously with respect to the
83 merely informs the caller whether the cancelation request
84 was successfully queued.
86 After a canceled thread has terminated,
87 a join with that thread using
91 as the thread's exit status.
92 (Joining with a thread is the only way to know that cancelation
98 on error, it returns a nonzero error number.
102 No thread with the ID
106 For an explanation of the terms used in this section, see
112 Interface Attribute Value
116 .BR pthread_cancel ()
117 T} Thread safety MT-Safe
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
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:
142 thread_func(): started; cancelation disabled
143 main(): sending cancelation request
144 thread_func(): about to enable cancelation
145 main(): thread was canceled
150 .\" SRC BEGIN (pthread_cancel.c)
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\[aq]t
167 immediately react to a cancelation request. */
169 s = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
171 handle_error_en(s, "pthread_setcancelstate");
173 printf("%s(): started; cancelation disabled\en", __func__);
175 printf("%s(): about to enable cancelation\en", __func__);
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("%s(): not canceled!\en", __func__);
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("%s(): sending cancelation request\en", __func__);
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("%s(): thread was canceled\en", __func__);
220 printf("%s(): thread wasn\[aq]t canceled (shouldn\[aq]t happen!)\en",
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),