OSDN Git Service

4323abf74e9caaff9af60489fdb0eaf5a07e7ef8
[linuxjm/LDP_man-pages.git] / release / 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 .\"*******************************************************************
27 .\"
28 .\" This file was generated with po4a. Translate the source file.
29 .\"
30 .\"*******************************************************************
31 .\"
32 .\" Japanese Version Copyright (c) 2012  Akihiro MOTOKI
33 .\"         all rights reserved.
34 .\" Translated 2012-05-03, Akihiro MOTOKI <amotoki@gmail.com>
35 .\"
36 .TH PTHREAD_CANCEL 3 2008\-11\-17 Linux "Linux Programmer's Manual"
37 .SH 名前
38 pthread_cancel \- スレッドにキャンセル要求を送る
39 .SH 書式
40 .nf
41 \fB#include <pthread.h>\fP
42
43 \fBint pthread_cancel(pthread_t \fP\fIthread\fP\fB);\fP
44 .sp
45 \fI\-pthread\fP でコンパイルしてリンクする。
46 .fi
47 .SH 説明
48 \fBpthread_cancel\fP() 関数は指定されたスレッド \fIthread\fP にキャンセル要求
49 を送信する。対象のスレッドがキャンセル要求に反応するかどうか、いつ反応
50 するかは対象のスレッドの制御下にある 2 つの属性、cancelability
51 \fIstate\fP と \fItype\fPにより決まる。
52
53 あるスレッドの cancelability state は \fBpthread_setcancelstate\fP(3) で
54 設定され、\fIenabled\fP と \fIdisabled\fP のいずれとなる (\fIenabled\fP が新しい
55 スレッドのデフォルト値である)。スレッドがキャンセルを無効にしていた場合、
56 キャンセル要求はそのスレッドがキャンセルを有効にするまでキューに入れら
57 れたままになる。スレッドがキャンセルを有効にしていた場合、
58 cancelability type によって、いつキャンセルが発生するかが決まる。
59
60 スレッドのキャンセル種別 (cancelability type) は
61 \fBpthread_setcanceltype\fP(3) で設定され、\fIasynchronous\fP か \fIdeferred\fP
62 のいずれかとなる(\fIdeferred\fP が新しいスレッドのデフォルト値である)。
63 非同期キャンセル (asynchronous cancelability) は、そのスレッドはいつで
64 もキャンセルされることを意味する (通常はすぐにキャンセルされるが、
65 システムがそのことを保証しているわけではない)。遅延キャンセル
66 (deferred cancelability) では、そのスレッドが \fI取り消しポイント
67 (cancellation point)\fP となっている関数を次に呼び出すまでキャンセルが
68 遅延される。取り消しポイントに設定されていたり設定
69 したりできる関数のリストは \fBpthreads\fP(7) に記載している。
70
71 キャンセル要求が実行されると、
72 \fIthread\fP では以下のステップが (この順序で) 行われる。
73 .IP 1. 3
74 キャンセルクリーンアップハンドラが (push されたのと逆順で)
75 取り出され (pop され)、呼び出される。
76 (\fBpthread_cleanup_push\fP(3) 参照)
77 .IP 2.
78 スレッド固有データのデストラクタ (destructor) が呼び出される。
79 呼び出し順序は規定されていない。
80 (\fBpthread_key_create\fP(3) 参照)
81 .IP 3.
82 スレッドが終了される。 (\fBpthread_exit\fP(3) 参照)
83 .PP
84 上記のステップは \fBpthread_cancel\fP() の呼び出しとは非同期に行われる。
85 \fBpthread_cancel\fP() の返却ステータスは単にキャンセル要求が正常に
86 キューに入れられたかどうかを呼び出し元に示すだけのものである。
87 .PP
88 キャンセルされたスレッドが終了された後に、
89 \fBpthread_join\fP(3) でそのスレッドを join すると、
90 そのスレッドの終了ステータスとして \fBPTHREAD_CANCELED\fP が得られる。
91 (スレッドの join はキャンセルが完了したかを知る唯一の方法である)
92 .SH 返り値
93 成功すると、 \fBpthread_cancel\fP() は 0 を返す。
94 エラーの場合、0 以外のエラー番号を返す。
95 .SH エラー
96 .TP 
97 \fBESRCH\fP
98 .\" .SH VERSIONS
99 .\" Available since glibc 2.0
100 ID が \fIthread\fP のスレッドが見つからなかった。
101 .SH 準拠
102 POSIX.1\-2001.
103 .SH 注意
104 Linux では、キャンセルはシグナルを使って実装されている。NPTL スレッド実装では、
105 最初のリアルタイムシグナル (つまり、シグナル 32)がこのために使用される。
106 LinuxThreads では、リアルタイムシグナルが利用可能な場合は2 番目のリアルタイム
107 シグナルが使用され、そうでない場合は \fBSIGUSR2\fP が使用される。
108 .SH 例
109 以下のプログラムは、スレッドを一つ作成してから、そのスレッドをキャンセルする。
110 メインスレッドはキャンセルされたスレッドをジョインし、
111 キャンセルされたスレッドの終了ステータスが \fBPTHREAD_CANCELED\fP かどうかを
112 確認する。以下のシェルセッションはこのプログラムを実行した際の実行例である。
113
114 .in +4n
115 .nf
116 $ ./a.out
117 thread_func(): started; cancellation disabled
118 main(): sending cancellation request
119 thread_func(): about to enable cancellation
120 main(): thread was canceled
121 .fi
122 .in
123 .SS プログラムのソース
124 \&
125 .nf
126 #include <pthread.h>
127 #include <stdio.h>
128 #include <errno.h>
129 #include <stdlib.h>
130 #include <unistd.h>
131
132 #define handle_error_en(en, msg) \e
133         do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
134
135 static void *
136 thread_func(void *ignored_argument)
137 {
138     int s;
139
140     /* Disable cancellation for a while, so that we don\(aqt
141        immediately react to a cancellation request */
142
143     s = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
144     if (s != 0)
145         handle_error_en(s, "pthread_setcancelstate");
146
147     printf("thread_func(): started; cancellation disabled\en");
148     sleep(5);
149     printf("thread_func(): about to enable cancellation\en");
150
151     s = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
152     if (s != 0)
153         handle_error_en(s, "pthread_setcancelstate");
154
155     /* sleep() is a cancellation point */
156
157     sleep(1000);        /* Should get canceled while we sleep */
158
159     /* Should never get here */
160
161     printf("thread_func(): not canceled!\en");
162     return NULL;
163 }
164
165 int
166 main(void)
167 {
168     pthread_t thr;
169     void *res;
170     int s;
171
172     /* Start a thread and then send it a cancellation request */
173
174     s = pthread_create(&thr, NULL, &thread_func, NULL);
175     if (s != 0)
176         handle_error_en(s, "pthread_create");
177
178     sleep(2);           /* Give thread a chance to get started */
179
180     printf("main(): sending cancellation request\en");
181     s = pthread_cancel(thr);
182     if (s != 0)
183         handle_error_en(s, "pthread_cancel");
184
185     /* Join with thread to see what its exit status was */
186
187     s = pthread_join(thr, &res);
188     if (s != 0)
189         handle_error_en(s, "pthread_join");
190
191     if (res == PTHREAD_CANCELED)
192         printf("main(): thread was canceled\en");
193     else
194         printf("main(): thread wasn\(aqt canceled (shouldn\(aqt happen!)\en");
195     exit(EXIT_SUCCESS);
196 }
197 .fi
198 .SH 関連項目
199 .ad l
200 .nh
201 \fBpthread_cleanup_push\fP(3), \fBpthread_create\fP(3), \fBpthread_exit\fP(3),
202 \fBpthread_join\fP(3), \fBpthread_key_create\fP(3), \fBpthread_setcancelstate\fP(3),
203 \fBpthread_setcanceltype\fP(3), \fBpthread_testcancel\fP(3), \fBpthreads\fP(7)
204 .SH この文書について
205 この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.65 の一部
206 である。プロジェクトの説明とバグ報告に関する情報は
207 http://www.kernel.org/doc/man\-pages/ に書かれている。