OSDN Git Service

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