1 .\" Copyright (C) 1996-1999 Free Software Foundation, Inc.
3 .\" Permission is granted to make and distribute verbatim copies of
4 .\" this manual provided the copyright notice and this permission notice are
5 .\" preserved on all copies.
7 .\" Permission is granted to copy and distribute modified versions of
8 .\" this manual under the conditions for verbatim copying, provided that
9 .\" the entire resulting derived work is distributed under the terms of a
10 .\" permission notice identical to this one.
12 .\" Permission is granted to copy and distribute translations of this
13 .\" manual into another language, under the above conditions for modified
14 .\" versions, except that this permission notice may be stated in a
15 .\" translation approved by the Foundation.
17 .\" Copyright (C) 1996 Xavier Leroy.
19 .\" Japanese Version Copyright (C) 2002-2003 Suzuki Takashi
20 .\" all rights reserved.
21 .\" Translated Tue Dec 31 23:11:09 JST 2002
22 .\" by Suzuki Takashi.
24 .\"WORD: thread-specific data スレッド固有データ
25 .\"WORD: thread-specific key スレッド固有キー
26 .\"WORD: destructor function デストラクタ関数
29 .TH PTHREAD_SPECIFIC 3 LinuxThreads
32 pthread_key_create, pthread_key_delete, pthread_setspecific, pthread_getspecific \- スレッド固有データの管理
35 .B #include <pthread.h>
37 .BI "int pthread_key_create(pthread_key_t *" key ", void (*" destr_function ") (void *));"
39 .BI "int pthread_key_delete(pthread_key_t " key ");"
41 .BI "int pthread_setspecific(pthread_key_t " key ", const void *" pointer ");"
43 .BI "void * pthread_getspecific(pthread_key_t " key ");"
48 グローバル変数や静的変数がしばしば必要となる。
49 複数のスレッドは 1 つのメモリ空間を共有するため、
50 通常の変数ではこれを実現することができない。
52 この必要性への POSIX スレッドの答えである。
54 それぞれのスレッドはスレッド固有データ (thread-specific data) 領域、
56 プライベートなメモリブロックを保有している。
57 この領域は TSD キーをインデックスとして管理される。
61 TSD キーはすべてのスレッドに共通であるが、
62 TSD キーに結び付けられる値はスレッドごとに異なるように
68 TSD キーはこの配列に対する整数値のインデックスとして、
69 TSD キーに結び付けられる値は呼び出しスレッドの対応する配列要素として見える。
71 スレッドが生成されると、TSD 領域はすべてのキーに対する値が
75 .B "pthread_key_create"
80 ある時点で確保できるキーの数には制限があり、
85 その時点で実行されているスレッドすべてにおいて
94 そのキーに対応するデストラクタ関数を登録することができる。
98 そのスレッド中でキーに結び付けられた値を引数として関数
106 スレッド終了時にデストラクタ関数が呼び出される順序は不定である。
109 現在のスレッドにおいてキーに結び付けられる値は
114 以外の値をそのキーやほかのキーに結び付けるかもしれない。
118 の値に対するデストラクタ関数をすべて呼び出したあとに
122 デストラクタ関数の呼び出し処理は繰り返される。
124 .B "PTHREAD_DESTRUCTOR_ITERATIONS"
125 回繰り返すと、たとえデストラクタ関数のある非
128 処理は中止される。LinuxThreads 以外の実装では無限ループに陥るかもしれない。
130 .B "pthread_key_delete"
134 の値が結び付けられているかどうかをチェックしたり、
135 キーに対応するデストラクタ関数を呼び出したりはしない。
137 .B "pthread_setspecific"
144 .B "pthread_getspecific"
151 .B "pthread_key_create"
153 .B "pthread_key_delete"
155 .B "pthread_setspecific"
156 は成功すると 0 を、失敗すると非 0 のエラーコードを返す。
158 .B "pthread_key_create"
164 .B "pthread_getspecific"
173 .B "pthread_key_create"
174 はエラーの場合に次のようなエラーコードを返す:
178 .B "PTHREAD_KEYS_MAX"
182 .B "pthread_key_delete"
184 .B "pthread_setspecific"
185 はエラーの場合に次のようなエラーコードを返す:
190 は有効な、確保された TSD キーではない。
193 .B "pthread_getspecific"
196 が有効な、確保された TSD キーでない場合には
201 Xavier Leroy <Xavier.Leroy@inria.fr>
204 pthread_create(3), pthread_exit(3), pthread_testcancel(3).
208 次のコードでは、100 バイトのスレッド固有の配列を確保し、
216 static pthread_key_t buffer_key;
219 static pthread_once_t buffer_key_once = PTHREAD_ONCE_INIT;
221 /* スレッド固有のバッファを確保する */
222 void buffer_alloc(void)
224 pthread_once(&buffer_key_once, buffer_key_alloc);
225 pthread_setspecific(buffer_key, malloc(100));
229 char * get_buffer(void)
231 return (char *) pthread_getspecific(buffer_key);
235 static void buffer_key_alloc()
237 pthread_key_create(&buffer_key, buffer_destroy);
240 /* スレッド固有のバッファを解放する */
241 static void buffer_destroy(void * buf)