OSDN Git Service

(split) LDP: Update original to LDP v3.38.
[linuxjm/LDP_man-pages.git] / original / man3 / pthread_attr_init.3
1 .\" Copyright (c) 2008 Linux Foundation, written by Michael Kerrisk
2 .\"     <mtk.manpages@gmail.com>
3 .\"
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.
7 .\"
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.
12 .\"
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
19 .\" professionally.
20 .\"
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
23 .\"
24 .TH PTHREAD_ATTR_INIT 3 2008-11-11 "Linux" "Linux Programmer's Manual"
25 .SH NAME
26 pthread_attr_init, pthread_attr_destroy \- initialize and destroy
27 thread attributes object
28 .SH SYNOPSIS
29 .nf
30 .B #include <pthread.h>
31
32 .BI "int pthread_attr_init(pthread_attr_t *" attr );
33 .BI "int pthread_attr_destroy(pthread_attr_t *" attr );
34 .sp
35 Compile and link with \fI\-pthread\fP.
36 .fi
37 .SH DESCRIPTION
38 The
39 .BR pthread_attr_init ()
40 function initializes the thread attributes object pointed to by
41 .IR attr
42 with default attribute values.
43 After this call, individual attributes of the object can be set
44 using various related functions (listed under SEE ALSO),
45 and then the object can be used in one or more
46 .BR pthread_create (3)
47 calls that create threads.
48
49 Calling
50 .BR pthread_attr_init ()
51 on a thread attributes object that has already been initialized
52 results in undefined behavior.
53
54 When a thread attributes object is no longer required,
55 it should be destroyed using the
56 .BR pthread_attr_destroy ()
57 function.
58 Destroying a thread attributes object has no effect
59 on threads that were created using that object.
60
61 Once a thread attributes object has been destroyed,
62 it can be reinitialized using
63 .BR pthread_attr_init ().
64 Any other use of a destroyed thread attributes object
65 has undefined results.
66 .SH RETURN VALUE
67 On success, these functions return 0;
68 on error, they return a nonzero error number.
69 .SH ERRORS
70 POSIX.1-2001 documents an
71 .B ENOMEM
72 error for
73 .BR pthread_attr_init ();
74 on Linux these functions always succeed
75 (but portable and future-proof applications should nevertheless
76 handle a possible error return).
77 .SH CONFORMING TO
78 POSIX.1-2001.
79 .SH NOTES
80 The
81 .I pthread_attr_t
82 type should be treated as opaque:
83 any access to the object other than via pthreads functions
84 is nonportable and produces undefined results.
85 .SH EXAMPLE
86 The program below optionally makes use of
87 .BR pthread_attr_init ()
88 and various related functions to initialize a thread attributes
89 object that is used to create a single thread.
90 Once created, the thread uses the
91 .BR pthread_getattr_np (3)
92 function (a nonstandard GNU extension) to retrieve the thread's
93 attributes, and then displays those attributes.
94
95 If the program is run with no command-line argument,
96 then it passes NULL as the
97 .I attr
98 argument of
99 .BR pthread_create (3),
100 so that the thread is created with default attributes.
101 Running the program on Linux/x86-32 with the NPTL threading implementation,
102 we see the following:
103
104 .in +4n
105 .nf
106 .\" Results from glibc 2.8, SUSE 11.0; Oct 2008
107 .RB "$" " ulimit \-s" "       # No stack imit ==> default stack size is 2MB"
108 unlimited
109 .RB "$" " ./a.out"
110 Thread attributes:
111         Detach state        = PTHREAD_CREATE_JOINABLE
112         Scope               = PTHREAD_SCOPE_SYSTEM
113         Inherit scheduler   = PTHREAD_INHERIT_SCHED
114         Scheduling policy   = SCHED_OTHER
115         Scheduling priority = 0
116         Guard size          = 4096 bytes
117         Stack address       = 0x40196000
118         Stack size          = 0x201000 bytes
119 .fi
120 .in
121
122 When we supply a stack size as a command-line argument,
123 the program initializes a thread attributes object,
124 sets various attributes in that object,
125 and passes a pointer to the object in the call to
126 .BR pthread_create (3).
127 Running the program on Linux/x86-32 with the NPTL threading implementation,
128 we see the following:
129
130 .in +4n
131 .nf
132 .\" Results from glibc 2.8, SUSE 11.0; Oct 2008
133 .RB "$" " ./a.out 0x3000000"
134 posix_memalign() allocated at 0x40197000
135 Thread attributes:
136         Detach state        = PTHREAD_CREATE_DETACHED
137         Scope               = PTHREAD_SCOPE_SYSTEM
138         Inherit scheduler   = PTHREAD_EXPLICIT_SCHED
139         Scheduling policy   = SCHED_OTHER
140         Scheduling priority = 0
141         Guard size          = 0 bytes
142         Stack address       = 0x40197000
143         Stack size          = 0x3000000 bytes
144 .fi
145 .in
146 .SS Program source
147 \&
148 .nf
149 #define _GNU_SOURCE     /* To get pthread_getattr_np() declaration */
150 #include <pthread.h>
151 #include <stdio.h>
152 #include <stdlib.h>
153 #include <unistd.h>
154 #include <errno.h>
155
156 #define handle_error_en(en, msg) \\
157         do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
158
159 static void
160 display_pthread_attr(pthread_attr_t *attr, char *prefix)
161 {
162     int s, i;
163     size_t v;
164     void *stkaddr;
165     struct sched_param sp;
166
167     s = pthread_attr_getdetachstate(attr, &i);
168     if (s != 0)
169         handle_error_en(s, "pthread_attr_getdetachstate");
170     printf("%sDetach state        = %s\\n", prefix,
171             (i == PTHREAD_CREATE_DETACHED) ? "PTHREAD_CREATE_DETACHED" :
172             (i == PTHREAD_CREATE_JOINABLE) ? "PTHREAD_CREATE_JOINABLE" :
173             "???");
174
175     s = pthread_attr_getscope(attr, &i);
176     if (s != 0)
177         handle_error_en(s, "pthread_attr_getscope");
178     printf("%sScope               = %s\\n", prefix,
179             (i == PTHREAD_SCOPE_SYSTEM)  ? "PTHREAD_SCOPE_SYSTEM" :
180             (i == PTHREAD_SCOPE_PROCESS) ? "PTHREAD_SCOPE_PROCESS" :
181             "???");
182
183     s = pthread_attr_getinheritsched(attr, &i);
184     if (s != 0)
185         handle_error_en(s, "pthread_attr_getinheritsched");
186     printf("%sInherit scheduler   = %s\\n", prefix,
187             (i == PTHREAD_INHERIT_SCHED)  ? "PTHREAD_INHERIT_SCHED" :
188             (i == PTHREAD_EXPLICIT_SCHED) ? "PTHREAD_EXPLICIT_SCHED" :
189             "???");
190
191     s = pthread_attr_getschedpolicy(attr, &i);
192     if (s != 0)
193         handle_error_en(s, "pthread_attr_getschedpolicy");
194     printf("%sScheduling policy   = %s\\n", prefix,
195             (i == SCHED_OTHER) ? "SCHED_OTHER" :
196             (i == SCHED_FIFO)  ? "SCHED_FIFO" :
197             (i == SCHED_RR)    ? "SCHED_RR" :
198             "???");
199
200     s = pthread_attr_getschedparam(attr, &sp);
201     if (s != 0)
202         handle_error_en(s, "pthread_attr_getschedparam");
203     printf("%sScheduling priority = %d\\n", prefix, sp.sched_priority);
204
205     s = pthread_attr_getguardsize(attr, &v);
206     if (s != 0)
207         handle_error_en(s, "pthread_attr_getguardsize");
208     printf("%sGuard size          = %d bytes\\n", prefix, v);
209
210     s = pthread_attr_getstack(attr, &stkaddr, &v);
211     if (s != 0)
212         handle_error_en(s, "pthread_attr_getstack");
213     printf("%sStack address       = %p\\n", prefix, stkaddr);
214     printf("%sStack size          = 0x%x bytes\\n", prefix, v);
215 }
216
217 static void *
218 thread_start(void *arg)
219 {
220     int s;
221     pthread_attr_t gattr;
222
223     /* pthread_getattr_np() is a non\-standard GNU extension that
224        retrieves the attributes of the thread specified in its
225        first argument */
226
227     s = pthread_getattr_np(pthread_self(), &gattr);
228     if (s != 0)
229         handle_error_en(s, "pthread_getattr_np");
230
231     printf("Thread attributes:\\n");
232     display_pthread_attr(&gattr, "\\t");
233
234     exit(EXIT_SUCCESS);         /* Terminate all threads */
235 }
236
237 int
238 main(int argc, char *argv[])
239 {
240     pthread_t thr;
241     pthread_attr_t attr;
242     pthread_attr_t *attrp;      /* NULL or &attr */
243     int s;
244
245     attrp = NULL;
246
247     /* If a command\-line argument was supplied, use it to set the
248        stack\-size attribute and set a few other thread attributes,
249        and set attrp pointing to thread attributes object */
250
251     if (argc > 1) {
252         int stack_size;
253         void *sp;
254
255         attrp = &attr;
256
257         s = pthread_attr_init(&attr);
258         if (s != 0)
259             handle_error_en(s, "pthread_attr_init");
260
261         s = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
262         if (s != 0)
263             handle_error_en(s, "pthread_attr_setdetachstate");
264
265         s = pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
266         if (s != 0)
267             handle_error_en(s, "pthread_attr_setinheritsched");
268
269         stack_size = strtoul(argv[1], NULL, 0);
270
271         s = posix_memalign(&sp, sysconf(_SC_PAGESIZE), stack_size);
272         if (s != 0)
273             handle_error_en(s, "posix_memalign");
274
275         printf("posix_memalign() allocated at %p\\n", sp);
276
277         s = pthread_attr_setstack(&attr, sp, stack_size);
278         if (s != 0)
279             handle_error_en(s, "pthread_attr_setstack");
280     }
281
282     s = pthread_create(&thr, attrp, &thread_start, NULL);
283     if (s != 0)
284         handle_error_en(s, "pthread_create");
285
286     if (attrp != NULL) {
287         s = pthread_attr_destroy(attrp);
288         if (s != 0)
289             handle_error_en(s, "pthread_attr_destroy");
290     }
291
292     pause();    /* Terminates when other thread calls exit() */
293 }
294 .fi
295 .SH SEE ALSO
296 .BR pthread_attr_setaffinity_np (3),
297 .BR pthread_attr_setdetachstate (3),
298 .BR pthread_attr_setguardsize (3),
299 .BR pthread_attr_setinheritsched (3),
300 .BR pthread_attr_setschedparam (3),
301 .BR pthread_attr_setschedpolicy (3),
302 .BR pthread_attr_setscope (3),
303 .BR pthread_attr_setstack (3),
304 .BR pthread_attr_setstackaddr (3),
305 .BR pthread_attr_setstacksize (3),
306 .BR pthread_create (3),
307 .BR pthread_getattr_np (3),
308 .BR pthreads (7)