OSDN Git Service

(split) LDP: Update the version to 3.53 in PO files
[linuxjm/LDP_man-pages.git] / release / man3 / pthread_attr_init.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_ATTR_INIT 3 2008\-11\-11 Linux "Linux Programmer's Manual"
32 .SH 名前
33 pthread_attr_init, pthread_attr_destroy \-
34 スレッド属性オブジェクトの初期化と破棄を行う
35 .SH 書式
36 .nf
37 \fB#include <pthread.h>\fP
38
39 \fBint pthread_attr_init(pthread_attr_t *\fP\fIattr\fP\fB);\fP
40 \fBint pthread_attr_destroy(pthread_attr_t *\fP\fIattr\fP\fB);\fP
41 .sp
42 \fI\-pthread\fP でコンパイルしてリンクする。
43 .fi
44 .SH 説明
45 \fBpthread_attr_init\fP() 関数は \fIattr\fP が指すスレッド属性オブジェクト
46 (thread attributes object) をデフォルトの属性値で初期化する。
47 この呼び出しの後、オブジェクトの各属性は (関連項目に載っている) 種々の
48 関数を使って設定することができ、このオブジェクトはスレッドの作成を行う
49 \fBpthread_create\fP(3) の呼び出しにおいて使用することができる (一つの
50 オブジェクトを複数の \fBpthread_create\fP(3) に渡してもよい)。
51
52 すでに初期化されているスレッド属性オブジェクトに対して
53 \fBpthread_attr_init\fP() を呼び出した場合、
54 どのような動作になるかは不定である。
55
56 スレッド属性オブジェクトがもはや必要なくなった際には、
57 そのオブジェクトは \fBpthread_attr_destroy\fP() 関数を使って破棄すべきである。
58 スレッド属性オブジェクトを破棄しても、
59 そのオブジェクトを使って作成されたスレッドには影響はない。
60
61 いったん破棄したスレッド属性オブジェクトは、
62 \fBpthread_attr_init\fP() を使って再初期化することができる。
63 破棄したスレッド属性オブジェクトをこれ以外の用途で
64 使った場合の結果は不定である。
65 .SH 返り値
66 成功すると、これらの関数は 0 を返す。
67 エラーの場合、0 以外のエラー番号を返す。
68 .SH エラー
69 POSIX.1\-2001 では \fBpthread_attr_init\fP() にはエラー \fBENOMEM\fP が
70 記載されている。
71 Linux では、これらの関数は常に成功する
72 (ただし、移植性や将来も動作することを保証したいアプリケーションでは
73 正のエラーの返り値を処理するようにすべきである)。
74 .SH 準拠
75 POSIX.1\-2001.
76 .SH 注意
77 \fIpthread_attr_t\fP 型の内部構造は意識すべきではない。
78 pthreads 関数経由以外でのオブジェクトへのアクセスは移植性がなく、
79 どのような結果が得られるかも分からない。
80 .SH 例
81 下記のプログラムは、\fBpthread_attr_init\fP() と種々の関連関数を使って、
82 スレッド属性オブジェクトの初期化を行い、
83 そのオブジェクトを使ってスレッドを一つ作成する。
84 作成されたスレッドは、作成後に \fBpthread_getattr_np\fP(3) 関数
85 (非標準の GNU 拡張) を使ってスレッドの属性を取得し、
86 取得した属性を表示する。
87
88 コマンドライン引き数なしでプログラムを実行した場合、
89 \fBpthread_create\fP(3) の \fIattr\fP 引き数には NULL が渡される。
90 この場合、スレッドはデフォルトの属性で作成される。
91 このプログラムを NPTL スレッド実装が使われている Linux/x86\-32 で
92 動作させると、以下のような出力が得られる。
93
94 .in +4n
95 .nf
96 .\" Results from glibc 2.8, SUSE 11.0; Oct 2008
97 $\fB ulimit \-s\fP       # No stack imit ==> default stack size is 2MB
98 unlimited
99 $\fB ./a.out\fP
100 Thread attributes:
101         Detach state        = PTHREAD_CREATE_JOINABLE
102         Scope               = PTHREAD_SCOPE_SYSTEM
103         Inherit scheduler   = PTHREAD_INHERIT_SCHED
104         Scheduling policy   = SCHED_OTHER
105         Scheduling priority = 0
106         Guard size          = 4096 bytes
107         Stack address       = 0x40196000
108         Stack size          = 0x201000 bytes
109 .fi
110 .in
111
112 コマンドライン引き数でスタックサイズが与えられた場合、
113 このプログラムは、スレッド属性オブジェクトを初期化し、
114 そのオブジェクトの各種属性を設定し、
115 \fBpthread_create\fP(3) の呼び出しでこのオブジェクトへのポインタを渡す。
116 このプログラムを NPTL スレッド実装が使われている Linux/x86\-32 で
117 動作させると、以下のような出力が得られる。
118
119 .in +4n
120 .nf
121 .\" Results from glibc 2.8, SUSE 11.0; Oct 2008
122 $\fB ./a.out 0x3000000\fP
123 posix_memalign() allocated at 0x40197000
124 Thread attributes:
125         Detach state        = PTHREAD_CREATE_DETACHED
126         Scope               = PTHREAD_SCOPE_SYSTEM
127         Inherit scheduler   = PTHREAD_EXPLICIT_SCHED
128         Scheduling policy   = SCHED_OTHER
129         Scheduling priority = 0
130         Guard size          = 0 bytes
131         Stack address       = 0x40197000
132         Stack size          = 0x3000000 bytes
133 .fi
134 .in
135 .SS プログラムのソース
136 \&
137 .nf
138 #define _GNU_SOURCE     /* To get pthread_getattr_np() declaration */
139 #include <pthread.h>
140 #include <stdio.h>
141 #include <stdlib.h>
142 #include <unistd.h>
143 #include <errno.h>
144
145 #define handle_error_en(en, msg) \e
146         do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
147
148 static void
149 display_pthread_attr(pthread_attr_t *attr, char *prefix)
150 {
151     int s, i;
152     size_t v;
153     void *stkaddr;
154     struct sched_param sp;
155
156     s = pthread_attr_getdetachstate(attr, &i);
157     if (s != 0)
158         handle_error_en(s, "pthread_attr_getdetachstate");
159     printf("%sDetach state        = %s\en", prefix,
160             (i == PTHREAD_CREATE_DETACHED) ? "PTHREAD_CREATE_DETACHED" :
161             (i == PTHREAD_CREATE_JOINABLE) ? "PTHREAD_CREATE_JOINABLE" :
162             "???");
163
164     s = pthread_attr_getscope(attr, &i);
165     if (s != 0)
166         handle_error_en(s, "pthread_attr_getscope");
167     printf("%sScope               = %s\en", prefix,
168             (i == PTHREAD_SCOPE_SYSTEM)  ? "PTHREAD_SCOPE_SYSTEM" :
169             (i == PTHREAD_SCOPE_PROCESS) ? "PTHREAD_SCOPE_PROCESS" :
170             "???");
171
172     s = pthread_attr_getinheritsched(attr, &i);
173     if (s != 0)
174         handle_error_en(s, "pthread_attr_getinheritsched");
175     printf("%sInherit scheduler   = %s\en", prefix,
176             (i == PTHREAD_INHERIT_SCHED)  ? "PTHREAD_INHERIT_SCHED" :
177             (i == PTHREAD_EXPLICIT_SCHED) ? "PTHREAD_EXPLICIT_SCHED" :
178             "???");
179
180     s = pthread_attr_getschedpolicy(attr, &i);
181     if (s != 0)
182         handle_error_en(s, "pthread_attr_getschedpolicy");
183     printf("%sScheduling policy   = %s\en", prefix,
184             (i == SCHED_OTHER) ? "SCHED_OTHER" :
185             (i == SCHED_FIFO)  ? "SCHED_FIFO" :
186             (i == SCHED_RR)    ? "SCHED_RR" :
187             "???");
188
189     s = pthread_attr_getschedparam(attr, &sp);
190     if (s != 0)
191         handle_error_en(s, "pthread_attr_getschedparam");
192     printf("%sScheduling priority = %d\en", prefix, sp.sched_priority);
193
194     s = pthread_attr_getguardsize(attr, &v);
195     if (s != 0)
196         handle_error_en(s, "pthread_attr_getguardsize");
197     printf("%sGuard size          = %d bytes\en", prefix, v);
198
199     s = pthread_attr_getstack(attr, &stkaddr, &v);
200     if (s != 0)
201         handle_error_en(s, "pthread_attr_getstack");
202     printf("%sStack address       = %p\en", prefix, stkaddr);
203     printf("%sStack size          = 0x%x bytes\en", prefix, v);
204 }
205
206 static void *
207 thread_start(void *arg)
208 {
209     int s;
210     pthread_attr_t gattr;
211
212     /* pthread_getattr_np() is a non\-standard GNU extension that
213        retrieves the attributes of the thread specified in its
214        first argument */
215
216     s = pthread_getattr_np(pthread_self(), &gattr);
217     if (s != 0)
218         handle_error_en(s, "pthread_getattr_np");
219
220     printf("Thread attributes:\en");
221     display_pthread_attr(&gattr, "\et");
222
223     exit(EXIT_SUCCESS);         /* Terminate all threads */
224 }
225
226 int
227 main(int argc, char *argv[])
228 {
229     pthread_t thr;
230     pthread_attr_t attr;
231     pthread_attr_t *attrp;      /* NULL or &attr */
232     int s;
233
234     attrp = NULL;
235
236     /* If a command\-line argument was supplied, use it to set the
237        stack\-size attribute and set a few other thread attributes,
238        and set attrp pointing to thread attributes object */
239
240     if (argc > 1) {
241         int stack_size;
242         void *sp;
243
244         attrp = &attr;
245
246         s = pthread_attr_init(&attr);
247         if (s != 0)
248             handle_error_en(s, "pthread_attr_init");
249
250         s = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
251         if (s != 0)
252             handle_error_en(s, "pthread_attr_setdetachstate");
253
254         s = pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
255         if (s != 0)
256             handle_error_en(s, "pthread_attr_setinheritsched");
257
258         stack_size = strtoul(argv[1], NULL, 0);
259
260         s = posix_memalign(&sp, sysconf(_SC_PAGESIZE), stack_size);
261         if (s != 0)
262             handle_error_en(s, "posix_memalign");
263
264         printf("posix_memalign() allocated at %p\en", sp);
265
266         s = pthread_attr_setstack(&attr, sp, stack_size);
267         if (s != 0)
268             handle_error_en(s, "pthread_attr_setstack");
269     }
270
271     s = pthread_create(&thr, attrp, &thread_start, NULL);
272     if (s != 0)
273         handle_error_en(s, "pthread_create");
274
275     if (attrp != NULL) {
276         s = pthread_attr_destroy(attrp);
277         if (s != 0)
278             handle_error_en(s, "pthread_attr_destroy");
279     }
280
281     pause();    /* Terminates when other thread calls exit() */
282 }
283 .fi
284 .SH 関連項目
285 .ad l
286 .nh
287 \fBpthread_attr_setaffinity_np\fP(3), \fBpthread_attr_setdetachstate\fP(3),
288 \fBpthread_attr_setguardsize\fP(3), \fBpthread_attr_setinheritsched\fP(3),
289 \fBpthread_attr_setschedparam\fP(3), \fBpthread_attr_setschedpolicy\fP(3),
290 \fBpthread_attr_setscope\fP(3), \fBpthread_attr_setstack\fP(3),
291 \fBpthread_attr_setstackaddr\fP(3), \fBpthread_attr_setstacksize\fP(3),
292 \fBpthread_create\fP(3), \fBpthread_getattr_np\fP(3), \fBpthreads\fP(7)
293 .SH この文書について
294 この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.53 の一部
295 である。プロジェクトの説明とバグ報告に関する情報は
296 http://www.kernel.org/doc/man\-pages/ に書かれている。