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_ATTR_INIT 3 2008\-11\-11 Linux "Linux Programmer's Manual"
31 pthread_attr_init, pthread_attr_destroy \-
32 スレッド属性オブジェクトの初期化と破棄を行う
35 \fB#include <pthread.h>\fP
37 \fBint pthread_attr_init(pthread_attr_t *\fP\fIattr\fP\fB);\fP
38 \fBint pthread_attr_destroy(pthread_attr_t *\fP\fIattr\fP\fB);\fP
40 \fI\-pthread\fP でコンパイルしてリンクする。
43 \fBpthread_attr_init\fP() 関数は \fIattr\fP が指すスレッド属性オブジェクト
44 (thread attributes object) をデフォルトの属性値で初期化する。
45 この呼び出しの後、オブジェクトの各属性は (関連項目に載っている) 種々の
46 関数を使って設定することができ、このオブジェクトはスレッドの作成を行う
47 \fBpthread_create\fP(3) の呼び出しにおいて使用することができる (一つの
48 オブジェクトを複数の \fBpthread_create\fP(3) に渡してもよい)。
50 すでに初期化されているスレッド属性オブジェクトに対して
51 \fBpthread_attr_init\fP() を呼び出した場合、
54 スレッド属性オブジェクトがもはや必要なくなった際には、
55 そのオブジェクトは \fBpthread_attr_destroy\fP() 関数を使って破棄すべきである。
57 そのオブジェクトを使って作成されたスレッドには影響はない。
59 いったん破棄したスレッド属性オブジェクトは、
60 \fBpthread_attr_init\fP() を使って再初期化することができる。
61 破棄したスレッド属性オブジェクトをこれ以外の用途で
67 POSIX.1\-2001 では \fBpthread_attr_init\fP() にはエラー \fBENOMEM\fP が
69 Linux では、これらの関数は常に作成する
70 (ただし、移植性や将来も動作することを保証したいアプリケーションでは
71 正のエラーの返り値を処理するようにすべきである)。
75 \fIpthread_attr_t\fP 型の内部構造は意識すべきではない。
76 pthreads 関数経由以外でのオブジェクトへのアクセスは移植性がなく、
79 下記のプログラムは、\fBpthread_attr_init\fP() と種々の関連関数を使って、
81 そのオブジェクトを使ってスレッドを一つ作成する。
82 作成されたスレッドは、作成後に \fBpthread_getattr_np\fP(3) 関数
83 (非標準の GNU 拡張) を使ってスレッドの属性を取得し、
86 コマンドライン引き数なしでプログラムを実行した場合、
87 \fBpthread_create\fP(3) の \fIattr\fP 引き数には NULL が渡される。
88 この場合、スレッドはデフォルトの属性で作成される。
89 このプログラムを NPTL スレッド実装が使われている Linux/x86\-32 で
94 .\" Results from glibc 2.8, SUSE 11.0; Oct 2008
95 $\fB ulimit \-s\fP # No stack imit ==> default stack size is 2MB
99 Detach state = PTHREAD_CREATE_JOINABLE
100 Scope = PTHREAD_SCOPE_SYSTEM
101 Inherit scheduler = PTHREAD_INHERIT_SCHED
102 Scheduling policy = SCHED_OTHER
103 Scheduling priority = 0
104 Guard size = 4096 bytes
105 Stack address = 0x40196000
106 Stack size = 0x201000 bytes
110 コマンドライン引き数でスタックサイズが与えられた場合、
111 このプログラムは、スレッド属性オブジェクトを初期化し、
113 \fBpthread_create\fP(3) の呼び出しでこのオブジェクトへのポインタを渡す。
114 このプログラムを NPTL スレッド実装が使われている Linux/x86\-32 で
115 動作させると、以下のような出力が得られる。
119 .\" Results from glibc 2.8, SUSE 11.0; Oct 2008
120 $\fB ./a.out 0x3000000\fP
121 posix_memalign() allocated at 0x40197000
123 Detach state = PTHREAD_CREATE_DETACHED
124 Scope = PTHREAD_SCOPE_SYSTEM
125 Inherit scheduler = PTHREAD_EXPLICIT_SCHED
126 Scheduling policy = SCHED_OTHER
127 Scheduling priority = 0
129 Stack address = 0x40197000
130 Stack size = 0x3000000 bytes
136 #define _GNU_SOURCE /* To get pthread_getattr_np() declaration */
143 #define handle_error_en(en, msg) \e
144 do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
147 display_pthread_attr(pthread_attr_t *attr, char *prefix)
152 struct sched_param sp;
154 s = pthread_attr_getdetachstate(attr, &i);
156 handle_error_en(s, "pthread_attr_getdetachstate");
157 printf("%sDetach state = %s\en", prefix,
158 (i == PTHREAD_CREATE_DETACHED) ? "PTHREAD_CREATE_DETACHED" :
159 (i == PTHREAD_CREATE_JOINABLE) ? "PTHREAD_CREATE_JOINABLE" :
162 s = pthread_attr_getscope(attr, &i);
164 handle_error_en(s, "pthread_attr_getscope");
165 printf("%sScope = %s\en", prefix,
166 (i == PTHREAD_SCOPE_SYSTEM) ? "PTHREAD_SCOPE_SYSTEM" :
167 (i == PTHREAD_SCOPE_PROCESS) ? "PTHREAD_SCOPE_PROCESS" :
170 s = pthread_attr_getinheritsched(attr, &i);
172 handle_error_en(s, "pthread_attr_getinheritsched");
173 printf("%sInherit scheduler = %s\en", prefix,
174 (i == PTHREAD_INHERIT_SCHED) ? "PTHREAD_INHERIT_SCHED" :
175 (i == PTHREAD_EXPLICIT_SCHED) ? "PTHREAD_EXPLICIT_SCHED" :
178 s = pthread_attr_getschedpolicy(attr, &i);
180 handle_error_en(s, "pthread_attr_getschedpolicy");
181 printf("%sScheduling policy = %s\en", prefix,
182 (i == SCHED_OTHER) ? "SCHED_OTHER" :
183 (i == SCHED_FIFO) ? "SCHED_FIFO" :
184 (i == SCHED_RR) ? "SCHED_RR" :
187 s = pthread_attr_getschedparam(attr, &sp);
189 handle_error_en(s, "pthread_attr_getschedparam");
190 printf("%sScheduling priority = %d\en", prefix, sp.sched_priority);
192 s = pthread_attr_getguardsize(attr, &v);
194 handle_error_en(s, "pthread_attr_getguardsize");
195 printf("%sGuard size = %d bytes\en", prefix, v);
197 s = pthread_attr_getstack(attr, &stkaddr, &v);
199 handle_error_en(s, "pthread_attr_getstack");
200 printf("%sStack address = %p\en", prefix, stkaddr);
201 printf("%sStack size = 0x%x bytes\en", prefix, v);
205 thread_start(void *arg)
208 pthread_attr_t gattr;
210 /* pthread_getattr_np() is a non\-standard GNU extension that
211 retrieves the attributes of the thread specified in its
214 s = pthread_getattr_np(pthread_self(), &gattr);
216 handle_error_en(s, "pthread_getattr_np");
218 printf("Thread attributes:\en");
219 display_pthread_attr(&gattr, "\et");
221 exit(EXIT_SUCCESS); /* Terminate all threads */
225 main(int argc, char *argv[])
229 pthread_attr_t *attrp; /* NULL or &attr */
234 /* If a command\-line argument was supplied, use it to set the
235 stack\-size attribute and set a few other thread attributes,
236 and set attrp pointing to thread attributes object */
244 s = pthread_attr_init(&attr);
246 handle_error_en(s, "pthread_attr_init");
248 s = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
250 handle_error_en(s, "pthread_attr_setdetachstate");
252 s = pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
254 handle_error_en(s, "pthread_attr_setinheritsched");
256 stack_size = strtoul(argv[1], NULL, 0);
258 s = posix_memalign(&sp, sysconf(_SC_PAGESIZE), stack_size);
260 handle_error_en(s, "posix_memalign");
262 printf("posix_memalign() allocated at %p\en", sp);
264 s = pthread_attr_setstack(&attr, sp, stack_size);
266 handle_error_en(s, "pthread_attr_setstack");
269 s = pthread_create(&thr, attrp, &thread_start, NULL);
271 handle_error_en(s, "pthread_create");
274 s = pthread_attr_destroy(attrp);
276 handle_error_en(s, "pthread_attr_destroy");
279 pause(); /* Terminates when other thread calls exit() */
283 \fBpthread_attr_setaffinity_np\fP(3), \fBpthread_attr_setdetachstate\fP(3),
284 \fBpthread_attr_setguardsize\fP(3), \fBpthread_attr_setinheritsched\fP(3),
285 \fBpthread_attr_setschedparam\fP(3), \fBpthread_attr_setschedpolicy\fP(3),
286 \fBpthread_attr_setscope\fP(3), \fBpthread_attr_setstack\fP(3),
287 \fBpthread_attr_setstackaddr\fP(3), \fBpthread_attr_setstacksize\fP(3),
288 \fBpthread_create\fP(3), \fBpthread_getattr_np\fP(3), \fBpthreads\fP(7)
290 この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.40 の一部
291 である。プロジェクトの説明とバグ報告に関する情報は
292 http://www.kernel.org/doc/man\-pages/ に書かれている。