OSDN Git Service

(split) LDP man-pages の original/ を v3.30 に更新。
[linuxjm/LDP_man-pages.git] / original / man3 / pthread_setcancelstate.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_SETCANCELSTATE 3 2008-11-24 "Linux" "Linux Programmer's Manual"
25 .SH NAME
26 pthread_setcancelstate, pthread_setcanceltype \-
27 set cancelability state and type
28 .SH SYNOPSIS
29 .nf
30 .B #include <pthread.h>
31
32 .BI "int pthread_setcancelstate(int " state ", int *" oldstate );
33 .BI "int pthread_setcanceltype(int " type ", int *" oldtype );
34 .sp
35 Compile and link with \fI\-pthread\fP.
36 .SH DESCRIPTION
37 The
38 .BR pthread_setcancelstate ()
39 sets the cancelability state of the calling thread to the value
40 given in
41 .IR state .
42 The previous cancelability state of the thread is returned
43 in the buffer pointed to by
44 .IR oldstate .
45 The
46 .I state
47 argument must have one of the following values:
48 .TP
49 .B PTHREAD_CANCEL_ENABLE
50 The thread is cancelable.
51 This is the default cancelability state in all new threads,
52 including the initial thread.
53 The thread's cancelability type determines when a cancelable thread
54 will respond to a cancellation request.
55 .TP
56 .B PTHREAD_CANCEL_DISABLE
57 The thread is not cancelable.
58 If a cancellation request is received,
59 it is blocked until cancelability is enabled.
60 .PP
61 The
62 .BR pthread_setcanceltype ()
63 sets the cancelability type of the calling thread to the value
64 given in
65 .IR type .
66 The previous cancelability type of the thread is returned
67 in the buffer pointed to by
68 .IR oldtype .
69 The
70 .I type
71 argument must have one of the following values:
72 .TP
73 .B PTHREAD_CANCEL_DEFERRED
74 A cancellation request is deferred until the thread next calls
75 a function that is a cancellation point (see
76 .BR pthreads (7)).
77 This is the default cancelability type in all new threads,
78 including the initial thread.
79 .TP
80 .B PTHREAD_CANCEL_ASYNCHRONOUS
81 The thread can be canceled at any time.
82 (Typically,
83 it will be canceled immediately upon receiving a cancellation request,
84 but the system doesn't guarantee this.)
85 .PP
86 The set-and-get operation performed by each of these functions
87 is atomic with respect to other threads in the process
88 calling the same function.
89 .SH RETURN VALUE
90 On success, these functions return 0;
91 on error, they return a nonzero error number.
92 .SH ERRORS
93 The
94 .BR pthread_setcancelstate ()
95 can fail with the following error:
96 .TP
97 .B EINVAL
98 Invalid value for
99 .IR state .
100 .PP
101 The
102 .BR pthread_setcanceltype ()
103 can fail with the following error:
104 .TP
105 .B EINVAL
106 Invalid value for
107 .IR type .
108 .\" .SH VERSIONS
109 .\" Available since glibc 2.0
110 .SH CONFORMING TO
111 POSIX.1-2001.
112 .SH NOTES
113 For details of what happens when a thread is canceled, see
114 .BR pthread_cancel (3).
115
116 Briefly disabling cancelability is useful
117 if a thread performs some critical action
118 that must not be interrupted by a cancellation request.
119 Beware of disabling cancelability for long periods,
120 or around operations that may block for long periods,
121 since that will render the thread unresponsive to cancellation requests.
122
123 Setting the cancelability type to
124 .B PTHREAD_CANCEL_ASYNCHRONOUS
125 is rarely useful.
126 Since the thread could be canceled at
127 .I any
128 time, it cannot safely reserve resources (e.g., allocating memory with
129 .BR malloc (3)),
130 acquire mutexes, semaphores, or locks, and so on.
131 Reserving resources is unsafe because the application has no way of
132 knowing what the state of these resources is when the thread is canceled;
133 that is, did cancellation occur before the resources were reserved,
134 while they were reserved, or after they were released?
135 Furthermore, some internal data structures
136 (e.g., the linked list of free blocks managed by the
137 .BR malloc (3)
138 family of functions) may be left in an inconsistent state
139 if cancellation occurs in the middle of the function call.
140 Consequently, clean-up handlers cease to be useful.
141 Functions that can be safely asynchronously canceled are called
142 .IR "async-cancel-safe functions" .
143 POSIX.1-2001 only requires that
144 .BR pthread_cancel (3),
145 .BR pthread_setcancelstate (),
146 and
147 .BR pthread_setcanceltype ()
148 be async-cancel-safe.
149 In general, other library functions
150 can't be safely called from an asynchronously cancelable thread.
151 One of the few circumstances in which asynchronous cancelability is useful
152 is for cancellation of a thread that is in a pure compute-bound loop.
153
154 The Linux threading implementations permit the
155 .I oldstate
156 argument of
157 .BR pthread_setcancelstate ()
158 to be NULL, in which case the information about the previous
159 cancelability state is not returned to the caller.
160 Many other implementations also permit a NULL
161 .I oldstat
162 argument,
163 .\" It looks like at least Solaris, FreeBSD and Tru64 support this.
164 but POSIX.1-2001 does not specify this point,
165 so portable applications should always specify a non-NULL value in
166 .IR oldstate .
167 A precisely analogous set of statements applies for the
168 .I oldtype
169 argument of
170 .BR pthread_setcanceltype ().
171 .SH EXAMPLE
172 See
173 .BR pthread_cancel (3).
174 .SH SEE ALSO
175 .BR pthread_cancel (3),
176 .BR pthread_cleanup_push (3),
177 .BR pthread_testcancel (3),
178 .BR pthreads (7)