2 .\" Copyright (c) 2008 Linux Foundation, written by Michael Kerrisk
3 .\" <mtk.manpages@gmail.com>
5 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
7 .TH pthread_attr_init 3 (date) "Linux man-pages (unreleased)"
9 pthread_attr_init, pthread_attr_destroy \- initialize and destroy
10 thread attributes object
13 .RI ( libpthread ", " \-lpthread )
16 .B #include <pthread.h>
18 .BI "int pthread_attr_init(pthread_attr_t *" attr );
19 .BI "int pthread_attr_destroy(pthread_attr_t *" attr );
23 .BR pthread_attr_init ()
24 function initializes the thread attributes object pointed to by
26 with default attribute values.
27 After this call, individual attributes of the object can be set
28 using various related functions (listed under SEE ALSO),
29 and then the object can be used in one or more
30 .BR pthread_create (3)
31 calls that create threads.
34 .BR pthread_attr_init ()
35 on a thread attributes object that has already been initialized
36 results in undefined behavior.
38 When a thread attributes object is no longer required,
39 it should be destroyed using the
40 .BR pthread_attr_destroy ()
42 Destroying a thread attributes object has no effect
43 on threads that were created using that object.
45 Once a thread attributes object has been destroyed,
46 it can be reinitialized using
47 .BR pthread_attr_init ().
48 Any other use of a destroyed thread attributes object
49 has undefined results.
51 On success, these functions return 0;
52 on error, they return a nonzero error number.
57 .BR pthread_attr_init ();
58 on Linux these functions always succeed
59 (but portable and future-proof applications should nevertheless
60 handle a possible error return).
62 For an explanation of the terms used in this section, see
68 Interface Attribute Value
72 .BR pthread_attr_init (),
73 .BR pthread_attr_destroy ()
74 T} Thread safety MT-Safe
83 type should be treated as opaque:
84 any access to the object other than via pthreads functions
85 is nonportable and produces undefined results.
87 The program below optionally makes use of
88 .BR pthread_attr_init ()
89 and various related functions to initialize a thread attributes
90 object that is used to create a single thread.
91 Once created, the thread uses the
92 .BR pthread_getattr_np (3)
93 function (a nonstandard GNU extension) to retrieve the thread's
94 attributes, and then displays those attributes.
96 If the program is run with no command-line argument,
97 then it passes NULL as the
100 .BR pthread_create (3),
101 so that the thread is created with default attributes.
102 Running the program on Linux/x86-32 with the NPTL threading implementation,
103 we see the following:
107 .\" Results from glibc 2.8, SUSE 11.0; Oct 2008
108 .RB "$" " ulimit \-s" " # No stack limit ==> default stack size is 2 MB"
112 Detach state = PTHREAD_CREATE_JOINABLE
113 Scope = PTHREAD_SCOPE_SYSTEM
114 Inherit scheduler = PTHREAD_INHERIT_SCHED
115 Scheduling policy = SCHED_OTHER
116 Scheduling priority = 0
117 Guard size = 4096 bytes
118 Stack address = 0x40196000
119 Stack size = 0x201000 bytes
123 When we supply a stack size as a command-line argument,
124 the program initializes a thread attributes object,
125 sets various attributes in that object,
126 and passes a pointer to the object in the call to
127 .BR pthread_create (3).
128 Running the program on Linux/x86-32 with the NPTL threading implementation,
129 we see the following:
133 .\" Results from glibc 2.8, SUSE 11.0; Oct 2008
134 .RB "$" " ./a.out 0x3000000"
135 posix_memalign() allocated at 0x40197000
137 Detach state = PTHREAD_CREATE_DETACHED
138 Scope = PTHREAD_SCOPE_SYSTEM
139 Inherit scheduler = PTHREAD_EXPLICIT_SCHED
140 Scheduling policy = SCHED_OTHER
141 Scheduling priority = 0
143 Stack address = 0x40197000
144 Stack size = 0x3000000 bytes
149 .\" SRC BEGIN (pthread_attr_init.c)
151 #define _GNU_SOURCE /* To get pthread_getattr_np() declaration */
160 display_pthread_attr(pthread_attr_t *attr, char *prefix)
165 struct sched_param sp;
167 s = pthread_attr_getdetachstate(attr, &i);
169 errc(EXIT_FAILURE, s, "pthread_attr_getdetachstate");
170 printf("%sDetach state = %s\en", 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 errc(EXIT_FAILURE, s, "pthread_attr_getscope");
178 printf("%sScope = %s\en", 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 errc(EXIT_FAILURE, s, "pthread_attr_getinheritsched");
186 printf("%sInherit scheduler = %s\en", 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 errc(EXIT_FAILURE, s, "pthread_attr_getschedpolicy");
194 printf("%sScheduling policy = %s\en", 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 errc(EXIT_FAILURE, s, "pthread_attr_getschedparam");
203 printf("%sScheduling priority = %d\en", prefix, sp.sched_priority);
205 s = pthread_attr_getguardsize(attr, &v);
207 errc(EXIT_FAILURE, s, "pthread_attr_getguardsize");
208 printf("%sGuard size = %zu bytes\en", prefix, v);
210 s = pthread_attr_getstack(attr, &stkaddr, &v);
212 errc(EXIT_FAILURE, s, "pthread_attr_getstack");
213 printf("%sStack address = %p\en", prefix, stkaddr);
214 printf("%sStack size = %#zx bytes\en", 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 errc(EXIT_FAILURE, s, "pthread_getattr_np");
231 printf("Thread attributes:\en");
232 display_pthread_attr(&gattr, "\et");
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 errc(EXIT_FAILURE, s, "pthread_attr_init");
261 s = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
263 errc(EXIT_FAILURE, s, "pthread_attr_setdetachstate");
265 s = pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
267 errc(EXIT_FAILURE, s, "pthread_attr_setinheritsched");
269 stack_size = strtoul(argv[1], NULL, 0);
271 s = posix_memalign(&sp, sysconf(_SC_PAGESIZE), stack_size);
273 errc(EXIT_FAILURE, s, "posix_memalign");
275 printf("posix_memalign() allocated at %p\en", sp);
277 s = pthread_attr_setstack(&attr, sp, stack_size);
279 errc(EXIT_FAILURE, s, "pthread_attr_setstack");
282 s = pthread_create(&thr, attrp, &thread_start, NULL);
284 errc(EXIT_FAILURE, s, "pthread_create");
287 s = pthread_attr_destroy(attrp);
289 errc(EXIT_FAILURE, s, "pthread_attr_destroy");
292 pause(); /* Terminates when other thread calls exit() */
299 .BR pthread_attr_setaffinity_np (3),
300 .BR pthread_attr_setdetachstate (3),
301 .BR pthread_attr_setguardsize (3),
302 .BR pthread_attr_setinheritsched (3),
303 .BR pthread_attr_setschedparam (3),
304 .BR pthread_attr_setschedpolicy (3),
305 .BR pthread_attr_setscope (3),
306 .BR pthread_attr_setsigmask_np (3),
307 .BR pthread_attr_setstack (3),
308 .BR pthread_attr_setstackaddr (3),
309 .BR pthread_attr_setstacksize (3),
310 .BR pthread_create (3),
311 .BR pthread_getattr_np (3),
312 .BR pthread_setattr_default_np (3),