OSDN Git Service

8038c2ffcb47c38427d13ed47cf6b8d65e15c8f1
[linuxjm/LDP_man-pages.git] / release / man3 / pthread_cleanup_push.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 .TH PTHREAD_CLEANUP_PUSH 3 2008\-11\-24 Linux "Linux Programmer's Manual"
32 .SH 名前
33 pthread_cleanup_push, pthread_cleanup_pop \- スレッドの
34 キャンセルクリーンアップハンドラの push/pop を行う
35 .SH 書式
36 .nf
37 \fB#include <pthread.h>\fP
38
39 \fBvoid pthread_cleanup_push(void (*\fP\fIroutine\fP\fB)(void *),\fP
40 \fB                          void *\fP\fIarg\fP\fB);\fP
41 \fBvoid pthread_cleanup_pop(int \fP\fIexecute\fP\fB);\fP
42 .sp
43 \fI\-pthread\fP でコンパイルしてリンクする。
44 .fi
45 .SH 説明
46 これらの関数は、呼び出したスレッドのスレッドキャンセル時のクリーンアッ
47 プハンドラのスタックの操作を行う。クリーンアップハンドラは、スレッドが
48 キャンセルされた場合 (や以下で説明する他の種々の状況において) 自動的に
49 実行される関数である。例えば、mutex のロック解除を行い、プロセス内の
50 他のスレッドが利用できるようにする関数などが考えられる。
51
52 \fBpthread_cleanup_push\fP() 関数は、 \fIroutine\fP をクリーンアップ
53 ハンドラのスタックの一番上にプッシュする。 \fIroutine\fP が後で
54 起動される際には、 \fIarg\fP が関数の引き数と渡される。
55
56 \fBpthread_cleanup_pop\fP() 関数は、クリーンアップハンドラの
57 スタックの一番上のルーチンを削除する。
58 \fIexecute\fP が 0 以外の場合にはそのルーチンを追加で実行する。
59
60 キャンセルクリーンアップハンドラは、以下に示す場合に
61 スタックから取り出され実行される。
62 .IP 1. 3
63 スレッドがキャンセルされた際に、スタックに登録された全てのクリーン
64 アップハンドラが取り出されて、実行される。クリーンアップハンドラの
65 実行は、スタックに登録されたのと逆の順序で行われる。
66 .IP 2.
67 スレッドが \fBpthread_exit\fP(3) を呼び出して終了する際に、全てのクリーン
68 アップハンドラが上の項目で述べたのと同様に実行される。
69 (スレッドがスレッド開始関数からの \fIreturn\fP の実行により終了する場合に
70 は、クリーンアップハンドラは\fI呼び出されない\fP。)
71 .IP 3.
72 スレッドが 0 以外の \fIexecute\fP 引き数で \fBpthread_cleanup_pop\fP() を
73 呼び出した際に、スタックの一番上のクリーンアップハンドラが取り出されて
74 実行される。
75 .PP
76 POSIX.1 では、 \fBpthread_cleanup_push\fP() と \fBpthread_cleanup_pop\fP() を
77 それぞれ \(aq\fB{\fP\(aq と \(aq\fB}\fP\(aq を含むテキストに展開するマクロと
78 して実装することを許容している。
79 このため、呼び出し側では、これらの関数の呼び出しが同じ関数の中で対と
80 なり、かつ文法的に同じネストレベル (nesting level) になることを保証
81 しなければならない。 (言い換えると、クリーンアップハンドラは、コード
82 の特定のセクションの実行の中でのみ設定するものであると言える。)
83
84 \fBlongjmp\fP(3) (\fBsiglongjmp\fP(3)) の呼び出しは、
85 \fBpthread_cleanup_push\fP() や \fBpthread_cleanup_pop\fP() の呼び出しが対と
86 なる呼び出しがない状態で行われた場合には、どのような結果になるかは不定
87 である。これは jump バッファは \fBsetjmp\fP(3) (\fBsigsetjmp\fP(3)) により設
88 定されるからである。同様に、クリーンアップハンドラ内からの
89 \fBlongjmp\fP(3) (\fBsiglongjmp\fP(3)) の呼び出しも、jump バッファがハンドラ
90 内で \fBsetjmp\fP(3) (\fBsigsetjmp\fP(3)) で設定されていない限り、どのような
91 結果になるかは不定である。
92 .SH 返り値
93 これらの関数は値を返さない。
94 .SH エラー
95 .\" SH VERSIONS
96 .\" Available since glibc 2.0
97 エラーはない。
98 .SH 準拠
99 POSIX.1\-2001.
100 .SH 注意
101 Linux では、関数 \fBpthread_cleanup_push\fP() と \fBpthread_cleanup_pop\fP()
102 は、それぞれ \(aq\fB{\fP\(aq と \(aq\fB}\fP\(aq を含むテキストに展開する
103 マクロとして実装されている。このことは、これらの関数を対で呼び出した
104 スコープ内で宣言された変数は、そのスコープの中でしか参照できない
105 ということを意味している。
106
107 .\" The text was actually added in the 2004 TC2
108 POSIX.1 には、括弧を含む \fBpthread_cleanup_push\fP() と
109 \fBpthread_cleanup_pop\fP() のブロックをそのままにしたままで、
110 \fIreturn\fP, \fIbreak\fP, \fIcontinue\fP, \fIgoto\fP を使った場合の影響は
111 不定であると書かれている。
112 移植性が必要なアプリケーションではこれを行うのは避けるべきである。
113 .SH 例
114 以下のプログラムは、このページで説明した関数の簡単な使用例を示すもので
115 ある。このプログラムは \fBpthread_cleanup_push\fP() と
116 \fBpthread_cleanup_pop\fP() で囲まれたループを実行するスレッドを作成する。
117 このループではグローバル変数 \fIcnt\fP を 1 秒に 1 ずつ増やしていく。
118 指定されたコマンドライン引き数の内容に基づいて、メインスレッドはもう一
119 つのスレッドにキャンセル要求を送ったり、もう一つのスレッドがループを
120 抜けて (\fIreturn\fP を呼び出して) 正常終了するようにグローバル変数を
121 設定したりする。
122
123 以下のシェルセッションでは、メインスレッドはもう一つのスレッドに
124 キャンセル要求を送信する。
125
126 .in +4n
127 .nf
128 $ \fB./a.out\fP
129 New thread started
130 cnt = 0
131 cnt = 1
132 Canceling thread
133 Called clean\-up handler
134 Thread was canceled; cnt = 0
135 .fi
136 .in
137
138 上記の実行例から、スレッドがキャンセルされ、
139 キャンセルクリーンアップハンドラが呼び出され、
140 グローバル変数 \fIcnt\fP の値が 0 にリセットされていることが確認できる。
141
142 次の実行例では、メインプログラムはグローバル変数を設定して、
143 もう一つのスレッドが正常終了するようにしている。
144
145 .in +4n
146 .nf
147 $ \fB./a.out x\fP
148 New thread started
149 cnt = 0
150 cnt = 1
151 Thread terminated normally; cnt = 2
152 .fi
153 .in
154
155 上記では、 (\fIcleanup_pop_arg\fP が 0 なので) クリーンアップハンドラは
156 実行されておらず、その結果 \fIcnt\fP の値はリセットされていないことが
157 分かる。
158
159 次の実行例では、メインプログラムはグローバル変数を設定して、
160 もう一つのスレッドが正常終了するようにし、さらに
161 \fIcleanup_pop_arg\fP に 0 以外の値を渡している。
162
163 .in +4n
164 .nf
165 $ \fB./a.out x 1\fP
166 New thread started
167 cnt = 0
168 cnt = 1
169 Called clean\-up handler
170 Thread terminated normally; cnt = 0
171 .fi
172 .in
173
174 上記では、スレッドはキャンセルされていないが、クリーンアップハンドラが
175 実行されていないことが分かる。これは \fBpthread_cleanup_pop\fP() の引き数
176 に 0 以外を渡したからである。
177 .SS プログラムのソース
178 \&
179 .nf
180 #include <pthread.h>
181 #include <sys/types.h>
182 #include <stdio.h>
183 #include <stdlib.h>
184 #include <unistd.h>
185 #include <errno.h>
186
187 #define handle_error_en(en, msg) \e
188         do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
189
190 static int done = 0;
191 static int cleanup_pop_arg = 0;
192 static int cnt = 0;
193
194 static void
195 cleanup_handler(void *arg)
196 {
197     printf("Called clean\-up handler\en");
198     cnt = 0;
199 }
200
201 static void *
202 thread_start(void *arg)
203 {
204     time_t start, curr;
205
206     printf("New thread started\en");
207
208     pthread_cleanup_push(cleanup_handler, NULL);
209
210     curr = start = time(NULL);
211
212     while (!done) {
213         pthread_testcancel();           /* A cancellation point */
214         if (curr < time(NULL)) {
215             curr = time(NULL);
216             printf("cnt = %d\en", cnt);  /* A cancellation point */
217             cnt++;
218         }
219     }
220
221     pthread_cleanup_pop(cleanup_pop_arg);
222     return NULL;
223 }
224
225 int
226 main(int argc, char *argv[])
227 {
228     pthread_t thr;
229     int s;
230     void *res;
231
232     s = pthread_create(&thr, NULL, thread_start, NULL);
233     if (s != 0)
234         handle_error_en(s, "pthread_create");
235
236     sleep(2);           /* Allow new thread to run a while */
237
238     if (argc > 1) {
239         if (argc > 2)
240             cleanup_pop_arg = atoi(argv[2]);
241         done = 1;
242
243     } else {
244         printf("Canceling thread\en");
245         s = pthread_cancel(thr);
246         if (s != 0)
247             handle_error_en(s, "pthread_cancel");
248     }
249
250     s = pthread_join(thr, &res);
251     if (s != 0)
252         handle_error_en(s, "pthread_join");
253
254     if (res == PTHREAD_CANCELED)
255         printf("Thread was canceled; cnt = %d\en", cnt);
256     else
257         printf("Thread terminated normally; cnt = %d\en", cnt);
258     exit(EXIT_SUCCESS);
259 }
260 .fi
261 .SH 関連項目
262 \fBpthread_cancel\fP(3), \fBpthread_cleanup_push_defer_np\fP(3),
263 \fBpthread_setcancelstate\fP(3), \fBpthread_testcancel\fP(3), \fBpthreads\fP(7)
264 .SH この文書について
265 この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.52 の一部
266 である。プロジェクトの説明とバグ報告に関する情報は
267 http://www.kernel.org/doc/man\-pages/ に書かれている。