1 .\" Copyright (c) 2008 Linux Foundation, written by Michael Kerrisk
2 .\" <mtk.manpages@gmail.com>
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.
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.
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
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
24 .\"*******************************************************************
26 .\" This file was generated with po4a. Translate the source file.
28 .\"*******************************************************************
29 .TH PTHREAD_CANCEL 3 2008\-11\-17 Linux "Linux Programmer's Manual"
31 pthread_cancel \- スレッドにキャンセル要求を送る
34 \fB#include <pthread.h>\fP
36 \fBint pthread_cancel(pthread_t \fP\fIthread\fP\fB);\fP
38 \fI\-pthread\fP でコンパイルしてリンクする。
41 \fBpthread_cancel\fP() 関数は指定されたスレッド \fIthread\fP にキャンセル要求を送信
42 する。対象のスレッドがキャンセル要求に反応するかどうか、いつ反応するかは対象
43 のスレッドの制御下にある 2 つの属性、cancelability \fIstate\fP と \fItype\fPにより
46 あるスレッドの cancelability state は \fBpthread_setcancelstate\fP(3) で設定され、
47 \fIenabled\fP と \fIdisabled\fP のいずれとなる (\fIenabled\fP が新しいスレッドのデフォ
48 ルト値である)。スレッドがキャンセルを無効にしていた場合、キャンセル要求はその
49 スレッドがキャンセルを有効にするまでキューに入れられたままになる。スレッドが
50 キャンセルを有効にしていた場合、cancelability type によって、いつキャンセルが
53 スレッドの cancellation type は \fBpthread_setcanceltype\fP(3) で設定され、
54 \fIasynchronous\fP か \fIdeferred\fP のいずれかとなる(\fIdeferred\fP が新しいスレッド
56 非同期キャンセル (asynchronous cancelability) は、そのスレッドはいつでも
57 キャンセルされることを意味する (通常はすぐにキャンセルされるが、システムが
59 遅延キャンセル (deferred cancelability) では、そのスレッドが \fIcancellation
60 point\fP となっている関数を次に呼び出すまでキャンセルが遅延される。
61 cancellation point に設定されていたり設定したりできる関数のリストは
62 \fIpthreads\fP(7) に記載している。
65 \fIthread\fP では以下のステップが (この順序で) 行われる。
67 キャンセルのクリーンアップハンドラが (push されたのと逆順で)
68 取り出され (pop され)、呼び出される。
69 (\fBpthread_cleanup_push\fP(3) 参照)
71 スレッド固有データのデストラクタ (destructor) が呼び出される。
73 (\fBpthread_key_create\fP(3) 参照)
75 スレッドが終了される。 (\fBpthread_exit\fP(3) 参照)
77 上記のステップは \fBpthread_cancel\fP() の呼び出しとは非同期に行われる。
78 \fBpthread_cancel\fP() の返却ステータスは単にキャンセル要求が正常に
79 キューに入れられたかどうかを呼び出し元に示すだけのものである。
82 \fBpthread_join\fP(3) でそのスレッドを join すると、
83 そのスレッドの終了ステータスとして \fBPTHREAD_CANCELED\fP が得られる。
84 (スレッドの join はキャンセルが完了したかを知る唯一の方法である)
86 成功すると、 \fBpthread_cancel\fP() は 0 を返す。
92 .\" Available since glibc 2.0
93 ID が \fIthread\fP のスレッドが見つからなかった。
97 Linux では、キャンセルはシグナルを使って実装されている。NPTL スレッド実装では、
98 最初のリアルタイムシグナル (つまり、シグナル 32)がこのために使用される。
99 LinuxThreads では、リアルタイムシグナルが利用可能な場合は2 番目のリアルタイム
100 シグナルが使用され、そうでない場合は \fBSIGUSR2\fP が使用される。
102 以下のプログラムは、スレッドを一つ作成してから、そのスレッドをキャンセルする。
103 メインスレッドはキャンセルされたスレッドをジョインし、
104 キャンセルされたスレッドの終了ステータスが \fBPTHREAD_CANCELED\fP かどうかを
105 確認する。以下のシェルセッションはこのプログラムを実行した際の実行例である。
110 thread_func(): started; cancellation disabled
111 main(): sending cancellation request
112 thread_func(): about to enable cancellation
113 main(): thread was canceled
125 #define handle_error_en(en, msg) \e
126 do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
129 thread_func(void *ignored_argument)
133 /* Disable cancellation for a while, so that we don\(aqt
134 immediately react to a cancellation request */
136 s = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
138 handle_error_en(s, "pthread_setcancelstate");
140 printf("thread_func(): started; cancellation disabled\en");
142 printf("thread_func(): about to enable cancellation\en");
144 s = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
146 handle_error_en(s, "pthread_setcancelstate");
148 /* sleep() is a cancellation point */
150 sleep(1000); /* Should get canceled while we sleep */
152 /* Should never get here */
154 printf("thread_func(): not canceled!\en");
165 /* Start a thread and then send it a cancellation request */
167 s = pthread_create(&thr, NULL, &thread_func, NULL);
169 handle_error_en(s, "pthread_create");
171 sleep(2); /* Give thread a chance to get started */
173 printf("main(): sending cancellation request\en");
174 s = pthread_cancel(thr);
176 handle_error_en(s, "pthread_cancel");
178 /* Join with thread to see what its exit status was */
180 s = pthread_join(thr, &res);
182 handle_error_en(s, "pthread_join");
184 if (res == PTHREAD_CANCELED)
185 printf("main(): thread was canceled\en");
187 printf("main(): thread wasn\(aqt canceled (shouldn\(aqt happen!)\en");
192 \fBpthread_cleanup_push\fP(3), \fBpthread_create\fP(3), \fBpthread_exit\fP(3),
193 \fBpthread_join\fP(3), \fBpthread_key_create\fP(3), \fBpthread_setcancelstate\fP(3),
194 \fBpthread_setcanceltype\fP(3), \fBpthread_testcancel\fP(3), \fBpthreads\fP(7)
196 この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.40 の一部
197 である。プロジェクトの説明とバグ報告に関する情報は
198 http://www.kernel.org/doc/man\-pages/ に書かれている。