1 .\" Copyright (c) 2008 Linux Foundation, written by Michael Kerrisk
2 .\" <mtk.manpages@gmail.com>
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date. The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein. The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
24 .TH PTHREAD_CANCEL 3 2008-11-17 "Linux" "Linux Programmer's Manual"
26 pthread_cancel \- send a cancellation request to a thread
29 .B #include <pthread.h>
31 .BI "int pthread_cancel(pthread_t " thread );
33 Compile and link with \fI\-pthread\fP.
38 function sends a cancellation request to the thread
40 Whether and when the target thread
41 reacts to the cancellation request depends on
42 two attributes that are under the control of that thread:
43 its cancelability \fIstate\fP and \fItype\fP.
45 A thread's cancelability state, determined by
46 .BR pthread_setcancelstate (3),
49 (the default for new threads) or
51 If a thread has disabled cancellation,
52 then a cancellation request remains queued until the thread
54 If a thread has enabled cancellation,
55 then its cancelability type determines when cancellation occurs.
57 A thread's cancellation type, determined by
58 .BR pthread_setcanceltype (3),
63 (the default for new threads).
64 Asynchronous cancelability
65 means that the thread can be canceled at any time
66 (usually immediately, but the system does not guarantee this).
67 Deferred cancelability means that cancellation will be delayed until
68 the thread next calls a function that is a
69 .IR "cancellation point" .
70 A list of functions that are or may be cancellation points is provided in
73 When a cancellation requested is acted on, the following steps occur for
77 Cancellation clean-up handlers are popped
78 (in the reverse of the order in which they were pushed) and called.
80 .BR pthread_cleanup_push (3).)
82 Thread-specific data destructors are called,
83 in an unspecified order.
85 .BR pthread_key_create (3).)
87 The thread is terminated.
89 .BR pthread_exit (3).)
91 The above steps happen asynchronously with respect to the
96 merely informs the caller whether the cancellation request
97 was successfully queued.
99 After a canceled thread has terminated,
100 a join with that thread using
104 as the thread's exit status.
105 (Joining with a thread is the only way to know that cancellation
109 .BR pthread_cancel ()
111 on error, it returns a nonzero error number.
115 No thread with the ID
119 .\" Available since glibc 2.0
123 On Linux, cancellation is implemented using signals.
124 Under the NPTL threading implementation,
125 the first real-time signal (i.e., signal 32) is used for this purpose.
126 On LinuxThreads, the second real-time signal is used,
127 if real-time signals are available, otherwise
131 The program below creates a thread and then cancels it.
132 The main thread joins with the canceled thread to check
133 that its exit status was
134 .BR PTHREAD_CANCELED .
135 The following shell session shows what happens when we run the program:
140 thread_func(): started; cancellation disabled
141 main(): sending cancellation request
142 thread_func(): about to enable cancellation
143 main(): thread was canceled
155 #define handle_error_en(en, msg) \\
156 do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
159 thread_func(void *ignored_argument)
163 /* Disable cancellation for a while, so that we don\(aqt
164 immediately react to a cancellation request */
166 s = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
168 handle_error_en(s, "pthread_setcancelstate");
170 printf("thread_func(): started; cancellation disabled\\n");
172 printf("thread_func(): about to enable cancellation\\n");
174 s = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
176 handle_error_en(s, "pthread_setcancelstate");
178 /* sleep() is a cancellation point */
180 sleep(1000); /* Should get canceled while we sleep */
182 /* Should never get here */
184 printf("thread_func(): not canceled!\\n");
195 /* Start a thread and then send it a cancellation request */
197 s = pthread_create(&thr, NULL, &thread_func, NULL);
199 handle_error_en(s, "pthread_create");
201 sleep(2); /* Give thread a chance to get started */
203 printf("main(): sending cancellation request\\n");
204 s = pthread_cancel(thr);
206 handle_error_en(s, "pthread_cancel");
208 /* Join with thread to see what its exit status was */
210 s = pthread_join(thr, &res);
212 handle_error_en(s, "pthread_join");
214 if (res == PTHREAD_CANCELED)
215 printf("main(): thread was canceled\\n");
217 printf("main(): thread wasn\(aqt canceled (shouldn\(aqt happen!)\\n");
222 .BR pthread_cleanup_push (3),
223 .BR pthread_create (3),
224 .BR pthread_exit (3),
225 .BR pthread_join (3),
226 .BR pthread_key_create (3),
227 .BR pthread_setcancelstate (3),
228 .BR pthread_setcanceltype (3),
229 .BR pthread_testcancel (3),