pthread_cancelのヘルプ・マニュアル
日本語 英語
pthread_cancel --help
man pthread_cancel
PTHREAD_CANCEL(3) PTHREAD_CANCEL(3)
NAME
pthread_cancel, pthread_setcancelstate, pthread_setcanceltype,
pthread_testcancel - スレッドの取り消し
書式
#include
int pthread_cancel(pthread_t thread);
int pthread_setcancelstate(int state, int *oldstate);
int pthread_setcanceltype(int type, int *oldtype);
void pthread_testcancel(void);
説明
取り消しは、あるスレッドが他のスレッドの実行を終了させることを可能に す
る メカニズムである。より正確には、スレッドは他のスレッドに対して取消要
求を送ることができる。設定次第で、目標のスレッドは、要求を無視したり 、
直 ちに実現したり、ある取り消しポイントに至るまでその要求の実行を延期し
たりできる。
スレッドが最終的に取り消し要求を実現する際に は 、 そ れ は あ た か も
pthread_exit(PTHREAD_CANCELED) がその時点で呼び出されたかのように振舞う
。すなわち、全てのクリーンアップハンドラが逆順に実行され、スレッド固 有
データの終了処理関数が呼び出され、最後にスレッドは、返り値 PTHREAD_CAN-
CEL で実行を停止する。詳しくは pthread_exit(3) を見よ。
pthread_cancel は thread 引数で指定されたスレッドに対して、取り消し要求
を送る。
pthread_setcancelstate は、これを呼び出すスレッドの取り消し状態を変更す
る。すなわち、取り消し要求を受け入れるか否かを変更する。 state 引数は新
た な 取 り 消 し 状 態 である。これは取り消しを可能にする PTHREAD_CAN-
CEL_ENABLE もしくは、取り消しを不可能にする(取り消し要求を 無 視 す る)
PTHREAD_CANCEL_DISABLE のいずれかである。 oldstate が NULL でなければ、
以前の取り消し状態が oldstate が指す場所に格納され、従って、後 で 別 の
pthread_setcancelstate の呼び出しにより、回復することができる。
pthread_setcanceltype は、これを呼び出すスレッドの取り消し要求に対する
反応の型を変更する。これは、非同期(即時)または遅延のいずれかで あ る 。
type 引数は、新たな取り消し方であり、取り消し要求が届くと直ちに呼び出し
スレッドを取り消す PTHREAD_CANCEL_ASYNCHRONOUS か、取り消し要求を次の取
り消しポイントまで留保する PTHREAD_CANCEL_DEFERRED かのいずれかである。
oldtype が NULL でなければ、以前の取り消し型が oldtype の指す場所に格納
され、従って、後から別の pthread_setcanceltype の呼び出しによって回復す
ることが可能である。
スレッドは常に pthread_create(3) によって、取り消し可能かつ遅延で作成さ
れる。すなわち、初期の取り消し状態は PTHREAD_CANCEL_ENABLE であり、初期
の型は PTHREAD_CANCEL_DEFERRED である。
取り消しポイントとは、保留中の取り消し要求に対するテストが行われ、実 際
に要求があれば取り消しが実行される点である。以下の POSIX スレッド関数は
取り消しポイントである:
pthread_join(3)
pthread_cond_wait(3)
pthread_cond_timedwait(3)
pthread_testcancel(3)
sem_wait(3)
sigwait(3)
これ以外の全ての POSIX スレッド関数は取り消しポイントではないことが保証
さ れている。すなわち、それらは遅延取り消しモードで決して取り消しを実現
することはない。
pthread_testcancel は保留中の取り消し要求を調べ、それを実現するだけであ
る 。その目的は、他に取り消しポイントとなる関数を呼び出すことのない、長
い連続したコードの中に、明示的に取り消しのチェックを導入することであ る
。
返り値
pthread_cancel、 pthread_setcancelstate および pthread_setcanceltype は
成功すると 0 を返し、エラーならば、非ゼロのエラーコードを返す。
エラー
pthread_cancel はエラーの際に次のエラーコードを返す:
ESRCH thread で指定されたものに対応するスレッドが存在しな い 。
ID.
pthread_setcancelstate はエラーの際に次のエラーコードを返す:
EINVAL state が PTHREAD_CANCEL_ENABLE でも PTHREAD_CANCEL_DIS-
ABLE でもない。
pthread_setcanceltype はエラーの際に次のエラーコードを返す:
EINVAL type 引数が PTHREAD_CANCEL_DEFERRED で も PTHREAD_CAN-
CEL_ASYNCHRONOUS でもない。
著者
Xavier Leroy
関連項目
pthread_exit(3), pthread_cleanup_push(3), pthread_cleanup_pop(3).
バグ
POSIX は一連のシステムコール(基本的には read(2), write(2), wait(2), 等
のようなブロックの可能性のある全ての関数)とそれらのシステムコールを呼ぶ
よ うなライブラリ関数(例えば fprintf(3)) が取り消しポイントであると規定
している。 LinuxThreads はこれを実装するには、まだ十分に C ライブラリと
統合されていると言えず、従っていかなる C ライブラリの関数も取り消しポイ
ントではない。
少なくとも、システムコールに対してはこれを回避する方法がある。取り消 し
要 求は、目標スレッドにシグナルを送ることによって送信される。このシグナ
ルはブロックしているシステムコール全てに対して割込みを掛け、それらは 直
ちに EINTR で戻る。よって、例えば read システムコールを呼んでいる間に取
り消しをチェックするには、次のようにすれば良い:
pthread_testcancel();
retcode = read(fd, buffer, length);
pthread_testcancel();
[訳注] 上の記述は glibc2 を用いたシステムでは正 し く な い 。 以 下 は
glib-2.1.2 の info ファイルからの引用である。
取 り消しポイントとは、保留中の取り消し要求に対するテストが行われ、実際
に要求があれば取り消しが実行される点である。POSIX スレッド関数 の う ち
、‘pthread_join’, ‘pthread_cond_wait’, ‘pthread_cond_timed_wait’,
‘pthread_testcancel’, ‘sem_wait’ 及び ‘sigwait’ は取り消しポイントで あ
る。 これに加えて、以下のシステムコールは取り消しポイントである:
accept open sendmsg
close pause sendto
connect read system
fcntl recv tcdrain
fsync recvfrom wait
lseek recvmsg waitpid
msync send write
nanosleep
こ れらの関数を呼び出す可能性のある printf() などのライブラリ関数も取り
消しポイントになる場合がある。
LinuxThreads PTHREAD_CANCEL(3)
PTHREAD_CANCEL(3) Linux Programmer’s Manual PTHREAD_CANCEL(3)
NAME
pthread_cancel - send a cancellation request to a thread
SYNOPSIS
#include
int pthread_cancel(pthread_t thread);
Compile and link with -pthread.
DESCRIPTION
The pthread_cancel() function sends a cancellation request to the
thread thread. Whether and when the target thread reacts to the can-
cellation request depends on two attributes that are under the control
of that thread: its cancelability state and type.
A thread’s cancelability state, determined by pthread_setcancel-
state(3), can be enabled (the default for new threads) or disabled. If
a thread has disabled cancellation, then a cancellation request remains
queued until the thread enables cancellation. If a thread has enabled
cancellation, then its cancelability type determines when cancellation
occurs.
A thread’s cancellation type, determined by pthread_setcanceltype(3),
may be either asynchronous or deferred (the default for new threads).
Asynchronous cancelability means that the thread can be canceled at any
time (usually immediately, but the system does not guarantee this).
Deferred cancelability means that cancellation will be delayed until
the thread next calls a function that is a cancellation point. A list
of functions that are or may be cancellation points is provided in
pthreads(7).
When a cancellation requested is acted on, the following steps occur
for thread (in this order):
1. Cancellation clean-up handlers are popped (in the reverse of the
order in which they were pushed) and called. (See
pthread_cleanup_push(3).)
2. Thread-specific data destructors are called, in an unspecified
order. (See pthread_key_create(3).)
3. The thread is terminated. (See pthread_exit(3).)
The above steps happen asynchronously with respect to the pthread_can-
cel() call; the return status of pthread_cancel() merely informs the
caller whether the cancellation request was successfully queued.
After a canceled thread has terminated, a join with that thread using
pthread_join(3) obtains PTHREAD_CANCELED as the thread’s exit status.
(Joining with a thread is the only way to know that cancellation has
completed.)
RETURN VALUE
On success, pthread_cancel() returns 0; on error, it returns a non-zero
error number.
ERRORS
ESRCH No thread with the ID thread could be found.
CONFORMING TO
POSIX.1-2001.
NOTES
On Linux, cancellation is implemented using signals. Under the NPTL
threading implementation, the first real-time signal (i.e., signal 32)
is used for this purpose. On LinuxThreads, the second real-time signal
is used, if real-time signals are available, otherwise SIGUSR2 is used.
EXAMPLE
The program below creates a thread and then cancels it. The main
thread joins with the canceled thread to check that its exit status was
PTHREAD_CANCELED. The following shell session shows what happens when
we run the program:
$ ./a.out
thread_func(): started; cancellation disabled
main(): sending cancellation request
thread_func(): about to enable cancellation
main(): thread was canceled
Program source
#include
#include
#include
#include
#include
#define handle_error_en(en, msg) \
do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
static void *
thread_func(void *ignored_argument)
{
int s;
/* Disable cancellation for a while, so that we don't
immediately react to a cancellation request */
s = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
if (s != 0)
handle_error_en(s, "pthread_setcancelstate");
printf("thread_func(): started; cancellation disabled\n");
sleep(5);
printf("thread_func(): about to enable cancellation\n");
s = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
if (s != 0)
handle_error_en(s, "pthread_setcancelstate");
/* sleep() is a cancellation point */
sleep(1000); /* Should get canceled while we sleep */
/* Should never get here */
printf("thread_func(): not canceled!\n");
return NULL;
}
int
main(void)
{
pthread_t thr;
void *res;
int s;
/* Start a thread and then send it a cancellation request */
s = pthread_create(&thr, NULL, &thread_func, NULL);
if (s != 0)
handle_error_en(s, "pthread_create");
sleep(2); /* Give thread a chance to get started */
printf("main(): sending cancellation request\n");
s = pthread_cancel(thr);
if (s != 0)
handle_error_en(s, "pthread_cancel");
/* Join with thread to see what its exit status was */
s = pthread_join(thr, &res);
if (s != 0)
handle_error_en(s, "pthread_join");
if (res == PTHREAD_CANCELED)
printf("main(): thread was canceled\n");
else
printf("main(): thread wasn't canceled (shouldn't happen!)\n");
exit(EXIT_SUCCESS);
}
SEE ALSO
pthread_cleanup_push(3), pthread_create(3), pthread_exit(3),
pthread_join(3), pthread_key_create(3), pthread_setcancelstate(3),
pthread_setcanceltype(3), pthread_testcancel(3), pthreads(7)
COLOPHON
This page is part of release 3.22 of the Linux man-pages project. A
description of the project, and information about reporting bugs, can
be found at http://www.kernel.org/doc/man-pages/.
Linux 2008-11-17 PTHREAD_CANCEL(3)