File: pthread_cleanup_push.3

package info (click to toggle)
manpages-ja 0.5.0.0.20210215%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 28,568 kB
  • sloc: perl: 161; makefile: 58
file content (272 lines) | stat: -rw-r--r-- 10,936 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
.\" Copyright (c) 2008 Linux Foundation, written by Michael Kerrisk
.\"     <mtk.manpages@gmail.com>
.\"
.\" %%%LICENSE_START(VERBATIM)
.\" Permission is granted to make and distribute verbatim copies of this
.\" manual provided the copyright notice and this permission notice are
.\" preserved on all copies.
.\"
.\" Permission is granted to copy and distribute modified versions of this
.\" manual under the conditions for verbatim copying, provided that the
.\" entire resulting derived work is distributed under the terms of a
.\" permission notice identical to this one.
.\"
.\" Since the Linux kernel and libraries are constantly changing, this
.\" manual page may be incorrect or out-of-date.  The author(s) assume no
.\" responsibility for errors or omissions, or for damages resulting from
.\" the use of the information contained herein.  The author(s) may not
.\" have taken the same level of care in the production of this manual,
.\" which is licensed free of charge, as they might when working
.\" professionally.
.\"
.\" Formatted or processed versions of this manual, if unaccompanied by
.\" the source, must acknowledge the copyright and authors of this work.
.\" %%%LICENSE_END
.\"
.\"*******************************************************************
.\"
.\" This file was generated with po4a. Translate the source file.
.\"
.\"*******************************************************************
.\"
.\" Japanese Version Copyright (c) 2012  Akihiro MOTOKI
.\"         all rights reserved.
.\" Translated 2012-06-04, Akihiro MOTOKI <amotoki@gmail.com>
.\"
.TH PTHREAD_CLEANUP_PUSH 3 2008\-11\-24 Linux "Linux Programmer's Manual"
.SH 名前
pthread_cleanup_push, pthread_cleanup_pop \- スレッドの
キャンセルクリーンアップハンドラーの push/pop を行う
.SH 書式
.nf
\fB#include <pthread.h>\fP

\fBvoid pthread_cleanup_push(void (*\fP\fIroutine\fP\fB)(void *),\fP
\fB                          void *\fP\fIarg\fP\fB);\fP
\fBvoid pthread_cleanup_pop(int \fP\fIexecute\fP\fB);\fP
.sp
\fI\-pthread\fP でコンパイルしてリンクする。
.fi
.SH 説明
これらの関数は、呼び出したスレッドのスレッドキャンセル時のクリーンアッ
プハンドラーのスタックの操作を行う。クリーンアップハンドラーは、スレッドが
キャンセルされた場合 (や以下で説明する他の種々の状況において) 自動的に
実行される関数である。例えば、mutex のロック解除を行い、プロセス内の
他のスレッドが利用できるようにする関数などが考えられる。

\fBpthread_cleanup_push\fP() 関数は、 \fIroutine\fP をクリーンアップ
ハンドラーのスタックの一番上にプッシュする。 \fIroutine\fP が後で
起動される際には、 \fIarg\fP が関数の引き数と渡される。

\fBpthread_cleanup_pop\fP() 関数は、クリーンアップハンドラーの
スタックの一番上のルーチンを削除する。
\fIexecute\fP が 0 以外の場合にはそのルーチンを追加で実行する。

キャンセルクリーンアップハンドラーは、以下に示す場合に
スタックから取り出され実行される。
.IP 1. 3
スレッドがキャンセルされた際に、スタックに登録された全てのクリーン
アップハンドラーが取り出されて、実行される。クリーンアップハンドラーの
実行は、スタックに登録されたのと逆の順序で行われる。
.IP 2.
スレッドが \fBpthread_exit\fP(3) を呼び出して終了する際に、全てのクリーン
アップハンドラーが上の項目で述べたのと同様に実行される。
(スレッドがスレッド開始関数からの \fIreturn\fP の実行により終了する場合に
は、クリーンアップハンドラーは\fI呼び出されない\fP。)
.IP 3.
スレッドが 0 以外の \fIexecute\fP 引き数で \fBpthread_cleanup_pop\fP() を
呼び出した際に、スタックの一番上のクリーンアップハンドラーが取り出されて
実行される。
.PP
POSIX.1 では、 \fBpthread_cleanup_push\fP() と \fBpthread_cleanup_pop\fP() を
それぞれ \(aq\fB{\fP\(aq と \(aq\fB}\fP\(aq を含むテキストに展開するマクロと
して実装することを許容している。
このため、呼び出し側では、これらの関数の呼び出しが同じ関数の中で対と
なり、かつ文法的に同じネストレベル (nesting level) になることを保証
しなければならない。 (言い換えると、クリーンアップハンドラーは、コード
の特定のセクションの実行の中でのみ設定するものであると言える。)

\fBlongjmp\fP(3) (\fBsiglongjmp\fP(3)) の呼び出しは、
\fBpthread_cleanup_push\fP() や \fBpthread_cleanup_pop\fP() の呼び出しが対と
なる呼び出しがない状態で行われた場合には、どのような結果になるかは不定
である。これは jump バッファーは \fBsetjmp\fP(3) (\fBsigsetjmp\fP(3)) により設
定されるからである。同様に、クリーンアップハンドラー内からの
\fBlongjmp\fP(3) (\fBsiglongjmp\fP(3)) の呼び出しも、jump バッファーがハンドラー
内で \fBsetjmp\fP(3) (\fBsigsetjmp\fP(3)) で設定されていない限り、どのような
結果になるかは不定である。
.SH 返り値
これらの関数は値を返さない。
.SH エラー
.\" SH VERSIONS
.\" Available since glibc 2.0
エラーはない。
.SH 準拠
POSIX.1\-2001.
.SH 注意
Linux では、関数 \fBpthread_cleanup_push\fP() と \fBpthread_cleanup_pop\fP()
は、それぞれ \(aq\fB{\fP\(aq と \(aq\fB}\fP\(aq を含むテキストに展開する
マクロとして実装されている。このことは、これらの関数を対で呼び出した
スコープ内で宣言された変数は、そのスコープの中でしか参照できない
ということを意味している。

.\" The text was actually added in the 2004 TC2
POSIX.1 には、括弧を含む \fBpthread_cleanup_push\fP() と
\fBpthread_cleanup_pop\fP() のブロックをそのままにしたままで、
\fIreturn\fP, \fIbreak\fP, \fIcontinue\fP, \fIgoto\fP を使った場合の影響は
不定であると書かれている。
移植性が必要なアプリケーションではこれを行うのは避けるべきである。
.SH 例
以下のプログラムは、このページで説明した関数の簡単な使用例を示すもので
ある。このプログラムは \fBpthread_cleanup_push\fP() と
\fBpthread_cleanup_pop\fP() で囲まれたループを実行するスレッドを作成する。
このループではグローバル変数 \fIcnt\fP を 1 秒に 1 ずつ増やしていく。
指定されたコマンドライン引き数の内容に基づいて、メインスレッドはもう一
つのスレッドにキャンセル要求を送ったり、もう一つのスレッドがループを
抜けて (\fIreturn\fP を呼び出して) 正常終了するようにグローバル変数を
設定したりする。

以下のシェルセッションでは、メインスレッドはもう一つのスレッドに
キャンセル要求を送信する。

.in +4n
.nf
$ \fB./a.out\fP
New thread started
cnt = 0
cnt = 1
Canceling thread
Called clean\-up handler
Thread was canceled; cnt = 0
.fi
.in

上記の実行例から、スレッドがキャンセルされ、
キャンセルクリーンアップハンドラーが呼び出され、
グローバル変数 \fIcnt\fP の値が 0 にリセットされていることが確認できる。

次の実行例では、メインプログラムはグローバル変数を設定して、
もう一つのスレッドが正常終了するようにしている。

.in +4n
.nf
$ \fB./a.out x\fP
New thread started
cnt = 0
cnt = 1
Thread terminated normally; cnt = 2
.fi
.in

上記では、 (\fIcleanup_pop_arg\fP が 0 なので) クリーンアップハンドラーは
実行されておらず、その結果 \fIcnt\fP の値はリセットされていないことが
分かる。

次の実行例では、メインプログラムはグローバル変数を設定して、
もう一つのスレッドが正常終了するようにし、さらに
\fIcleanup_pop_arg\fP に 0 以外の値を渡している。

.in +4n
.nf
$ \fB./a.out x 1\fP
New thread started
cnt = 0
cnt = 1
Called clean\-up handler
Thread terminated normally; cnt = 0
.fi
.in

上記では、スレッドはキャンセルされていないが、クリーンアップハンドラーが
実行されていないことが分かる。これは \fBpthread_cleanup_pop\fP() の引き数
に 0 以外を渡したからである。
.SS プログラムのソース
\&
.nf
#include <pthread.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>

#define handle_error_en(en, msg) \e
        do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)

static int done = 0;
static int cleanup_pop_arg = 0;
static int cnt = 0;

static void
cleanup_handler(void *arg)
{
    printf("Called clean\-up handler\en");
    cnt = 0;
}

static void *
thread_start(void *arg)
{
    time_t start, curr;

    printf("New thread started\en");

    pthread_cleanup_push(cleanup_handler, NULL);

    curr = start = time(NULL);

    while (!done) {
        pthread_testcancel();           /* A cancellation point */
        if (curr < time(NULL)) {
            curr = time(NULL);
            printf("cnt = %d\en", cnt);  /* A cancellation point */
            cnt++;
        }
    }

    pthread_cleanup_pop(cleanup_pop_arg);
    return NULL;
}

int
main(int argc, char *argv[])
{
    pthread_t thr;
    int s;
    void *res;

    s = pthread_create(&thr, NULL, thread_start, NULL);
    if (s != 0)
        handle_error_en(s, "pthread_create");

    sleep(2);           /* Allow new thread to run a while */

    if (argc > 1) {
        if (argc > 2)
            cleanup_pop_arg = atoi(argv[2]);
        done = 1;

    } else {
        printf("Canceling thread\en");
        s = pthread_cancel(thr);
        if (s != 0)
            handle_error_en(s, "pthread_cancel");
    }

    s = pthread_join(thr, &res);
    if (s != 0)
        handle_error_en(s, "pthread_join");

    if (res == PTHREAD_CANCELED)
        printf("Thread was canceled; cnt = %d\en", cnt);
    else
        printf("Thread terminated normally; cnt = %d\en", cnt);
    exit(EXIT_SUCCESS);
}
.fi
.SH 関連項目
\fBpthread_cancel\fP(3), \fBpthread_cleanup_push_defer_np\fP(3),
\fBpthread_setcancelstate\fP(3), \fBpthread_testcancel\fP(3), \fBpthreads\fP(7)
.SH この文書について
この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.79 の一部
である。プロジェクトの説明とバグ報告に関する情報は
http://www.kernel.org/doc/man\-pages/ に書かれている。