OSDN Git Service

LDP: Update original to LDP v3.79
[linuxjm/LDP_man-pages.git] / original / man3 / pthread_cancel.3
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 2008-11-17 "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
33 .BI "int pthread_cancel(pthread_t " thread );
34 .sp
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
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
62 A thread's cancellation type, determined by
63 .BR pthread_setcanceltype (3),
64 may be either
65 .IR asynchronous
66 or
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
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.
103 .PP
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
118 .TP
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 CONFORMING TO
126 POSIX.1-2001.
127 .SH NOTES
128 On Linux, cancellation is implemented using signals.
129 Under the NPTL threading implementation,
130 the first real-time signal (i.e., signal 32) is used for this purpose.
131 On LinuxThreads, the second real-time signal is used,
132 if real-time signals are available, otherwise
133 .B SIGUSR2
134 is used.
135 .SH EXAMPLE
136 The program below creates a thread and then cancels it.
137 The main thread joins with the canceled thread to check
138 that its exit status was
139 .BR PTHREAD_CANCELED .
140 The following shell session shows what happens when we run the program:
141
142 .in +4n
143 .nf
144 $ ./a.out
145 thread_func(): started; cancellation disabled
146 main(): sending cancellation request
147 thread_func(): about to enable cancellation
148 main(): thread was canceled
149 .fi
150 .in
151 .SS Program source
152 \&
153 .nf
154 #include <pthread.h>
155 #include <stdio.h>
156 #include <errno.h>
157 #include <stdlib.h>
158 #include <unistd.h>
159
160 #define handle_error_en(en, msg) \\
161         do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
162
163 static void *
164 thread_func(void *ignored_argument)
165 {
166     int s;
167
168     /* Disable cancellation for a while, so that we don\(aqt
169        immediately react to a cancellation request */
170
171     s = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
172     if (s != 0)
173         handle_error_en(s, "pthread_setcancelstate");
174
175     printf("thread_func(): started; cancellation disabled\\n");
176     sleep(5);
177     printf("thread_func(): about to enable cancellation\\n");
178
179     s = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
180     if (s != 0)
181         handle_error_en(s, "pthread_setcancelstate");
182
183     /* sleep() is a cancellation point */
184
185     sleep(1000);        /* Should get canceled while we sleep */
186
187     /* Should never get here */
188
189     printf("thread_func(): not canceled!\\n");
190     return NULL;
191 }
192
193 int
194 main(void)
195 {
196     pthread_t thr;
197     void *res;
198     int s;
199
200     /* Start a thread and then send it a cancellation request */
201
202     s = pthread_create(&thr, NULL, &thread_func, NULL);
203     if (s != 0)
204         handle_error_en(s, "pthread_create");
205
206     sleep(2);           /* Give thread a chance to get started */
207
208     printf("main(): sending cancellation request\\n");
209     s = pthread_cancel(thr);
210     if (s != 0)
211         handle_error_en(s, "pthread_cancel");
212
213     /* Join with thread to see what its exit status was */
214
215     s = pthread_join(thr, &res);
216     if (s != 0)
217         handle_error_en(s, "pthread_join");
218
219     if (res == PTHREAD_CANCELED)
220         printf("main(): thread was canceled\\n");
221     else
222         printf("main(): thread wasn\(aqt canceled (shouldn\(aqt happen!)\\n");
223     exit(EXIT_SUCCESS);
224 }
225 .fi
226 .SH SEE ALSO
227 .ad l
228 .nh
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)
238 .SH COLOPHON
239 This page is part of release 3.79 of the Linux
240 .I man-pages
241 project.
242 A description of the project,
243 information about reporting bugs,
244 and the latest version of this page,
245 can be found at
246 \%http://www.kernel.org/doc/man\-pages/.