share/mk/: Fix includes
[man-pages.git] / man3 / pthread_cancel.3
blob3fef2f1c5fd82a9d73c50d6597f98650dc20b3d2
1 '\" t
2 .\" Copyright (c) 2008 Linux Foundation, written by Michael Kerrisk
3 .\"     <mtk.manpages@gmail.com>
4 .\"
5 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
6 .\"
7 .TH pthread_cancel 3 (date) "Linux man-pages (unreleased)"
8 .SH NAME
9 pthread_cancel \- send a cancelation request to a thread
10 .SH LIBRARY
11 POSIX threads library
12 .RI ( libpthread ", " \-lpthread )
13 .SH SYNOPSIS
14 .nf
15 .B #include <pthread.h>
17 .BI "int pthread_cancel(pthread_t " thread );
18 .fi
19 .SH DESCRIPTION
20 The
21 .BR pthread_cancel ()
22 function sends a cancelation request to the thread
23 .IR 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:
27 its cancelability
28 .I state
29 and
30 .IR type .
32 A thread's cancelability state, determined by
33 .BR pthread_setcancelstate (3),
34 can be
35 .I enabled
36 (the default for new threads) or
37 .IR disabled .
38 If a thread has disabled cancelation,
39 then a cancelation request remains queued until the thread
40 enables cancelation.
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),
46 may be either
47 .I asynchronous
49 .I deferred
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
58 .BR pthreads (7).
60 When a cancelation requested is acted on, the following steps occur for
61 .I thread
62 (in this order):
63 .IP (1) 5
64 Cancelation clean-up handlers are popped
65 (in the reverse of the order in which they were pushed) and called.
66 (See
67 .BR pthread_cleanup_push (3).)
68 .IP (2)
69 Thread-specific data destructors are called,
70 in an unspecified order.
71 (See
72 .BR pthread_key_create (3).)
73 .IP (3)
74 The thread is terminated.
75 (See
76 .BR pthread_exit (3).)
78 The above steps happen asynchronously with respect to the
79 .BR pthread_cancel ()
80 call;
81 the return status of
82 .BR pthread_cancel ()
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
88 .BR pthread_join (3)
89 obtains
90 .B PTHREAD_CANCELED
91 as the thread's exit status.
92 (Joining with a thread is the only way to know that cancelation
93 has completed.)
94 .SH RETURN VALUE
95 On success,
96 .BR pthread_cancel ()
97 returns 0;
98 on error, it returns a nonzero error number.
99 .SH ERRORS
101 .B ESRCH
102 No thread with the ID
103 .I thread
104 could be found.
105 .SH ATTRIBUTES
106 For an explanation of the terms used in this section, see
107 .BR attributes (7).
109 allbox;
110 lbx lb lb
111 l l l.
112 Interface       Attribute       Value
116 .BR pthread_cancel ()
117 T}      Thread safety   MT-Safe
119 .SH VERSIONS
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
125 .B SIGUSR2
126 is used.
127 .SH STANDARDS
128 POSIX.1-2008.
129 .SH HISTORY
130 glibc 2.0
131 POSIX.1-2001.
132 .SH EXAMPLES
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:
139 .in +4n
141 $ ./a.out
142 thread_func(): started; cancelation disabled
143 main(): sending cancelation request
144 thread_func(): about to enable cancelation
145 main(): thread was canceled
148 .SS Program source
150 .\" SRC BEGIN (pthread_cancel.c)
152 #include <errno.h>
153 #include <pthread.h>
154 #include <stdio.h>
155 #include <stdlib.h>
156 #include <unistd.h>
158 #define handle_error_en(en, msg) \e
159         do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
161 static void *
162 thread_func(void *ignored_argument)
164     int s;
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);
170     if (s != 0)
171         handle_error_en(s, "pthread_setcancelstate");
173     printf("%s(): started; cancelation disabled\en", __func__);
174     sleep(5);
175     printf("%s(): about to enable cancelation\en", __func__);
177     s = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
178     if (s != 0)
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__);
188     return NULL;
192 main(void)
194     pthread_t thr;
195     void *res;
196     int s;
198     /* Start a thread and then send it a cancelation request. */
200     s = pthread_create(&thr, NULL, &thread_func, NULL);
201     if (s != 0)
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);
208     if (s != 0)
209         handle_error_en(s, "pthread_cancel");
211     /* Join with thread to see what its exit status was. */
213     s = pthread_join(thr, &res);
214     if (s != 0)
215         handle_error_en(s, "pthread_join");
217     if (res == PTHREAD_CANCELED)
218         printf("%s(): thread was canceled\en", __func__);
219     else
220         printf("%s(): thread wasn\[aq]t canceled (shouldn\[aq]t happen!)\en",
221                __func__);
222     exit(EXIT_SUCCESS);
225 .\" SRC END
226 .SH SEE ALSO
227 .ad l
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),
237 .BR pthreads (7)