OSDN Git Service

(split) LDP: Update original to LDP v3.38.
[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 .\" 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.
7 .\"
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.
12 .\"
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
19 .\" professionally.
20 .\"
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
23 .\"
24 .TH PTHREAD_CANCEL 3 2008-11-17 "Linux" "Linux Programmer's Manual"
25 .SH NAME
26 pthread_cancel \- send a cancellation request to a thread
27 .SH SYNOPSIS
28 .nf
29 .B #include <pthread.h>
30
31 .BI "int pthread_cancel(pthread_t " thread );
32 .sp
33 Compile and link with \fI\-pthread\fP.
34 .fi
35 .SH DESCRIPTION
36 The
37 .BR pthread_cancel ()
38 function sends a cancellation request to the thread
39 .IR 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.
44
45 A thread's cancelability state, determined by
46 .BR pthread_setcancelstate (3),
47 can be
48 .I enabled
49 (the default for new threads) or
50 .IR disabled .
51 If a thread has disabled cancellation,
52 then a cancellation request remains queued until the thread
53 enables cancellation.
54 If a thread has enabled cancellation,
55 then its cancelability type determines when cancellation occurs.
56
57 A thread's cancellation type, determined by
58 .BR pthread_setcanceltype (3),
59 may be either
60 .IR asynchronous
61 or
62 .IR deferred
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
71 .IR pthreads (7).
72
73 When a cancellation requested is acted on, the following steps occur for
74 .IR thread
75 (in this order):
76 .IP 1. 3
77 Cancellation clean-up handlers are popped
78 (in the reverse of the order in which they were pushed) and called.
79 (See
80 .BR pthread_cleanup_push (3).)
81 .IP 2.
82 Thread-specific data destructors are called,
83 in an unspecified order.
84 (See
85 .BR pthread_key_create (3).)
86 .IP 3.
87 The thread is terminated.
88 (See
89 .BR pthread_exit (3).)
90 .PP
91 The above steps happen asynchronously with respect to the
92 .BR pthread_cancel ()
93 call;
94 the return status of
95 .BR pthread_cancel ()
96 merely informs the caller whether the cancellation request
97 was successfully queued.
98 .PP
99 After a canceled thread has terminated,
100 a join with that thread using
101 .BR pthread_join (3)
102 obtains
103 .B PTHREAD_CANCELED
104 as the thread's exit status.
105 (Joining with a thread is the only way to know that cancellation
106 has completed.)
107 .SH RETURN VALUE
108 On success,
109 .BR pthread_cancel ()
110 returns 0;
111 on error, it returns a nonzero error number.
112 .SH ERRORS
113 .TP
114 .B ESRCH
115 No thread with the ID
116 .I thread
117 could be found.
118 .\" .SH VERSIONS
119 .\" Available since glibc 2.0
120 .SH CONFORMING TO
121 POSIX.1-2001.
122 .SH NOTES
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
128 .B SIGUSR2
129 is used.
130 .SH EXAMPLE
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:
136
137 .in +4n
138 .nf
139 $ ./a.out
140 thread_func(): started; cancellation disabled
141 main(): sending cancellation request
142 thread_func(): about to enable cancellation
143 main(): thread was canceled
144 .fi
145 .in
146 .SS Program source
147 \&
148 .nf
149 #include <pthread.h>
150 #include <stdio.h>
151 #include <errno.h>
152 #include <stdlib.h>
153 #include <unistd.h>
154
155 #define handle_error_en(en, msg) \\
156         do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
157
158 static void *
159 thread_func(void *ignored_argument)
160 {
161     int s;
162
163     /* Disable cancellation for a while, so that we don\(aqt
164        immediately react to a cancellation request */
165
166     s = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
167     if (s != 0)
168         handle_error_en(s, "pthread_setcancelstate");
169
170     printf("thread_func(): started; cancellation disabled\\n");
171     sleep(5);
172     printf("thread_func(): about to enable cancellation\\n");
173
174     s = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
175     if (s != 0)
176         handle_error_en(s, "pthread_setcancelstate");
177
178     /* sleep() is a cancellation point */
179
180     sleep(1000);        /* Should get canceled while we sleep */
181
182     /* Should never get here */
183
184     printf("thread_func(): not canceled!\\n");
185     return NULL;
186 }
187
188 int
189 main(void)
190 {
191     pthread_t thr;
192     void *res;
193     int s;
194
195     /* Start a thread and then send it a cancellation request */
196
197     s = pthread_create(&thr, NULL, &thread_func, NULL);
198     if (s != 0)
199         handle_error_en(s, "pthread_create");
200
201     sleep(2);           /* Give thread a chance to get started */
202
203     printf("main(): sending cancellation request\\n");
204     s = pthread_cancel(thr);
205     if (s != 0)
206         handle_error_en(s, "pthread_cancel");
207
208     /* Join with thread to see what its exit status was */
209
210     s = pthread_join(thr, &res);
211     if (s != 0)
212         handle_error_en(s, "pthread_join");
213
214     if (res == PTHREAD_CANCELED)
215         printf("main(): thread was canceled\\n");
216     else
217         printf("main(): thread wasn\(aqt canceled (shouldn\(aqt happen!)\\n");
218     exit(EXIT_SUCCESS);
219 }
220 .fi
221 .SH SEE ALSO
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),
230 .BR pthreads (7)