wait.2: Add ESRCH for when pid == INT_MIN
[man-pages.git] / man3 / pthread_cancel.3
blob01b7cf021051fae0d3f66049a56e60b55d529fa1
1 .\" Copyright (c) 2008 Linux Foundation, written by Michael Kerrisk
2 .\"     <mtk.manpages@gmail.com>
3 .\"
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.
8 .\"
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.
13 .\"
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
20 .\" professionally.
21 .\"
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
24 .\" %%%LICENSE_END
25 .\"
26 .TH PTHREAD_CANCEL 3 2021-03-22 "Linux" "Linux Programmer's Manual"
27 .SH NAME
28 pthread_cancel \- send a cancellation request to a thread
29 .SH SYNOPSIS
30 .nf
31 .B #include <pthread.h>
32 .PP
33 .BI "int pthread_cancel(pthread_t " thread );
34 .PP
35 Compile and link with \fI\-pthread\fP.
36 .fi
37 .SH DESCRIPTION
38 The
39 .BR pthread_cancel ()
40 function sends a cancellation request to the thread
41 .IR 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:
45 its cancelability
46 .I state
47 and
48 .IR type .
49 .PP
50 A thread's cancelability state, determined by
51 .BR pthread_setcancelstate (3),
52 can be
53 .I enabled
54 (the default for new threads) or
55 .IR disabled .
56 If a thread has disabled cancellation,
57 then a cancellation request remains queued until the thread
58 enables cancellation.
59 If a thread has enabled cancellation,
60 then its cancelability type determines when cancellation occurs.
61 .PP
62 A thread's cancellation type, determined by
63 .BR pthread_setcanceltype (3),
64 may be either
65 .IR asynchronous
67 .IR deferred
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
76 .BR pthreads (7).
77 .PP
78 When a cancellation requested is acted on, the following steps occur for
79 .IR thread
80 (in this order):
81 .IP 1. 3
82 Cancellation clean-up handlers are popped
83 (in the reverse of the order in which they were pushed) and called.
84 (See
85 .BR pthread_cleanup_push (3).)
86 .IP 2.
87 Thread-specific data destructors are called,
88 in an unspecified order.
89 (See
90 .BR pthread_key_create (3).)
91 .IP 3.
92 The thread is terminated.
93 (See
94 .BR pthread_exit (3).)
95 .PP
96 The above steps happen asynchronously with respect to the
97 .BR pthread_cancel ()
98 call;
99 the return status of
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
106 .BR pthread_join (3)
107 obtains
108 .B PTHREAD_CANCELED
109 as the thread's exit status.
110 (Joining with a thread is the only way to know that cancellation
111 has completed.)
112 .SH RETURN VALUE
113 On success,
114 .BR pthread_cancel ()
115 returns 0;
116 on error, it returns a nonzero error number.
117 .SH ERRORS
119 .B ESRCH
120 No thread with the ID
121 .I thread
122 could be found.
123 .\" .SH VERSIONS
124 .\" Available since glibc 2.0
125 .SH ATTRIBUTES
126 For an explanation of the terms used in this section, see
127 .BR attributes (7).
128 .ad l
131 allbox;
132 lbx lb lb
133 l l l.
134 Interface       Attribute       Value
136 .BR pthread_cancel ()
137 T}      Thread safety   MT-Safe
141 .sp 1
142 .SH CONFORMING TO
143 POSIX.1-2001, POSIX.1-2008.
144 .SH NOTES
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
150 .B SIGUSR2
151 is used.
152 .SH EXAMPLES
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:
159 .in +4n
161 $ ./a.out
162 thread_func(): started; cancellation disabled
163 main(): sending cancellation request
164 thread_func(): about to enable cancellation
165 main(): thread was canceled
168 .SS Program source
171 #include <pthread.h>
172 #include <stdio.h>
173 #include <errno.h>
174 #include <stdlib.h>
175 #include <unistd.h>
177 #define handle_error_en(en, msg) \e
178         do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
180 static void *
181 thread_func(void *ignored_argument)
183     int s;
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);
189     if (s != 0)
190         handle_error_en(s, "pthread_setcancelstate");
192     printf("thread_func(): started; cancellation disabled\en");
193     sleep(5);
194     printf("thread_func(): about to enable cancellation\en");
196     s = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
197     if (s != 0)
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");
207     return NULL;
211 main(void)
213     pthread_t thr;
214     void *res;
215     int s;
217     /* Start a thread and then send it a cancellation request. */
219     s = pthread_create(&thr, NULL, &thread_func, NULL);
220     if (s != 0)
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);
227     if (s != 0)
228         handle_error_en(s, "pthread_cancel");
230     /* Join with thread to see what its exit status was. */
232     s = pthread_join(thr, &res);
233     if (s != 0)
234         handle_error_en(s, "pthread_join");
236     if (res == PTHREAD_CANCELED)
237         printf("main(): thread was canceled\en");
238     else
239         printf("main(): thread wasn\(aqt canceled (shouldn\(aqt happen!)\en");
240     exit(EXIT_SUCCESS);
243 .SH SEE ALSO
244 .ad l
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),
254 .BR pthreads (7)