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 .TH PTHREAD_ATTR_INIT 3 2008-11-11 "Linux" "Linux Programmer's Manual"
26 pthread_attr_init, pthread_attr_destroy \- initialize and destroy
27 thread attributes object
30 .B #include <pthread.h>
32 .BI "int pthread_attr_init(pthread_attr_t *" attr );
33 .BI "int pthread_attr_destroy(pthread_attr_t *" attr );
35 Compile and link with \fI\-pthread\fP.
39 .BR pthread_attr_init ()
40 function initializes the thread attributes object pointed to by
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.
50 .BR pthread_attr_init ()
51 on a thread attributes object that has already been initialized
52 results in undefined behavior.
54 When a thread attributes object is no longer required,
55 it should be destroyed using the
56 .BR pthread_attr_destroy ()
58 Destroying a thread attributes object has no effect
59 on threads that were created using that object.
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.
67 On success, these functions return 0;
68 on error, they return a nonzero error number.
70 POSIX.1-2001 documents an
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).
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.
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.
95 If the program is run with no command-line argument,
96 then it passes NULL as the
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:
106 .\" Results from glibc 2.8, SUSE 11.0; Oct 2008
107 .RB "$" " ulimit \-s" " # No stack imit ==> default stack size is 2MB"
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
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:
132 .\" Results from glibc 2.8, SUSE 11.0; Oct 2008
133 .RB "$" " ./a.out 0x3000000"
134 posix_memalign() allocated at 0x40197000
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
142 Stack address = 0x40197000
143 Stack size = 0x3000000 bytes
149 #define _GNU_SOURCE /* To get pthread_getattr_np() declaration */
156 #define handle_error_en(en, msg) \\
157 do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
160 display_pthread_attr(pthread_attr_t *attr, char *prefix)
165 struct sched_param sp;
167 s = pthread_attr_getdetachstate(attr, &i);
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" :
175 s = pthread_attr_getscope(attr, &i);
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" :
183 s = pthread_attr_getinheritsched(attr, &i);
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" :
191 s = pthread_attr_getschedpolicy(attr, &i);
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" :
200 s = pthread_attr_getschedparam(attr, &sp);
202 handle_error_en(s, "pthread_attr_getschedparam");
203 printf("%sScheduling priority = %d\\n", prefix, sp.sched_priority);
205 s = pthread_attr_getguardsize(attr, &v);
207 handle_error_en(s, "pthread_attr_getguardsize");
208 printf("%sGuard size = %d bytes\\n", prefix, v);
210 s = pthread_attr_getstack(attr, &stkaddr, &v);
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);
218 thread_start(void *arg)
221 pthread_attr_t gattr;
223 /* pthread_getattr_np() is a non\-standard GNU extension that
224 retrieves the attributes of the thread specified in its
227 s = pthread_getattr_np(pthread_self(), &gattr);
229 handle_error_en(s, "pthread_getattr_np");
231 printf("Thread attributes:\\n");
232 display_pthread_attr(&gattr, "\\t");
234 exit(EXIT_SUCCESS); /* Terminate all threads */
238 main(int argc, char *argv[])
242 pthread_attr_t *attrp; /* NULL or &attr */
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 */
257 s = pthread_attr_init(&attr);
259 handle_error_en(s, "pthread_attr_init");
261 s = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
263 handle_error_en(s, "pthread_attr_setdetachstate");
265 s = pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
267 handle_error_en(s, "pthread_attr_setinheritsched");
269 stack_size = strtoul(argv[1], NULL, 0);
271 s = posix_memalign(&sp, sysconf(_SC_PAGESIZE), stack_size);
273 handle_error_en(s, "posix_memalign");
275 printf("posix_memalign() allocated at %p\\n", sp);
277 s = pthread_attr_setstack(&attr, sp, stack_size);
279 handle_error_en(s, "pthread_attr_setstack");
282 s = pthread_create(&thr, attrp, &thread_start, NULL);
284 handle_error_en(s, "pthread_create");
287 s = pthread_attr_destroy(attrp);
289 handle_error_en(s, "pthread_attr_destroy");
292 pause(); /* Terminates when other thread calls exit() */
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),