1 .\" Copyright (c) 2008 Linux Foundation, written by Michael Kerrisk
2 .\" <mtk.manpages@gmail.com>
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.
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.
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
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
26 .\"*******************************************************************
28 .\" This file was generated with po4a. Translate the source file.
30 .\"*******************************************************************
32 .\" Japanese Version Copyright (c) 2012 Akihiro MOTOKI
33 .\" all rights reserved.
34 .\" Translated 2012-05-03, Akihiro MOTOKI <amotoki@gmail.com>
36 .TH PTHREAD_CANCEL 3 2008\-11\-17 Linux "Linux Programmer's Manual"
38 pthread_cancel \- スレッドにキャンセル要求を送る
41 \fB#include <pthread.h>\fP
43 \fBint pthread_cancel(pthread_t \fP\fIthread\fP\fB);\fP
45 \fI\-pthread\fP でコンパイルしてリンクする。
48 \fBpthread_cancel\fP() 関数は指定されたスレッド \fIthread\fP にキャンセル要求
49 を送信する。対象のスレッドがキャンセル要求に反応するかどうか、いつ反応
50 するかは対象のスレッドの制御下にある 2 つの属性、cancelability
51 \fIstate\fP と \fItype\fPにより決まる。
53 あるスレッドの cancelability state は \fBpthread_setcancelstate\fP(3) で
54 設定され、\fIenabled\fP と \fIdisabled\fP のいずれとなる (\fIenabled\fP が新しい
55 スレッドのデフォルト値である)。スレッドがキャンセルを無効にしていた場合、
56 キャンセル要求はそのスレッドがキャンセルを有効にするまでキューに入れら
57 れたままになる。スレッドがキャンセルを有効にしていた場合、
58 cancelability type によって、いつキャンセルが発生するかが決まる。
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) に記載している。
72 \fIthread\fP では以下のステップが (この順序で) 行われる。
74 キャンセルクリーンアップハンドラが (push されたのと逆順で)
75 取り出され (pop され)、呼び出される。
76 (\fBpthread_cleanup_push\fP(3) 参照)
78 スレッド固有データのデストラクタ (destructor) が呼び出される。
80 (\fBpthread_key_create\fP(3) 参照)
82 スレッドが終了される。 (\fBpthread_exit\fP(3) 参照)
84 上記のステップは \fBpthread_cancel\fP() の呼び出しとは非同期に行われる。
85 \fBpthread_cancel\fP() の返却ステータスは単にキャンセル要求が正常に
86 キューに入れられたかどうかを呼び出し元に示すだけのものである。
89 \fBpthread_join\fP(3) でそのスレッドを join すると、
90 そのスレッドの終了ステータスとして \fBPTHREAD_CANCELED\fP が得られる。
91 (スレッドの join はキャンセルが完了したかを知る唯一の方法である)
93 成功すると、 \fBpthread_cancel\fP() は 0 を返す。
99 .\" Available since glibc 2.0
100 ID が \fIthread\fP のスレッドが見つからなかった。
104 Linux では、キャンセルはシグナルを使って実装されている。NPTL スレッド実装では、
105 最初のリアルタイムシグナル (つまり、シグナル 32)がこのために使用される。
106 LinuxThreads では、リアルタイムシグナルが利用可能な場合は2 番目のリアルタイム
107 シグナルが使用され、そうでない場合は \fBSIGUSR2\fP が使用される。
109 以下のプログラムは、スレッドを一つ作成してから、そのスレッドをキャンセルする。
110 メインスレッドはキャンセルされたスレッドをジョインし、
111 キャンセルされたスレッドの終了ステータスが \fBPTHREAD_CANCELED\fP かどうかを
112 確認する。以下のシェルセッションはこのプログラムを実行した際の実行例である。
117 thread_func(): started; cancellation disabled
118 main(): sending cancellation request
119 thread_func(): about to enable cancellation
120 main(): thread was canceled
132 #define handle_error_en(en, msg) \e
133 do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
136 thread_func(void *ignored_argument)
140 /* Disable cancellation for a while, so that we don\(aqt
141 immediately react to a cancellation request */
143 s = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
145 handle_error_en(s, "pthread_setcancelstate");
147 printf("thread_func(): started; cancellation disabled\en");
149 printf("thread_func(): about to enable cancellation\en");
151 s = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
153 handle_error_en(s, "pthread_setcancelstate");
155 /* sleep() is a cancellation point */
157 sleep(1000); /* Should get canceled while we sleep */
159 /* Should never get here */
161 printf("thread_func(): not canceled!\en");
172 /* Start a thread and then send it a cancellation request */
174 s = pthread_create(&thr, NULL, &thread_func, NULL);
176 handle_error_en(s, "pthread_create");
178 sleep(2); /* Give thread a chance to get started */
180 printf("main(): sending cancellation request\en");
181 s = pthread_cancel(thr);
183 handle_error_en(s, "pthread_cancel");
185 /* Join with thread to see what its exit status was */
187 s = pthread_join(thr, &res);
189 handle_error_en(s, "pthread_join");
191 if (res == PTHREAD_CANCELED)
192 printf("main(): thread was canceled\en");
194 printf("main(): thread wasn\(aqt canceled (shouldn\(aqt happen!)\en");
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)
205 この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.65 の一部
206 である。プロジェクトの説明とバグ報告に関する情報は
207 http://www.kernel.org/doc/man\-pages/ に書かれている。