1 .\" Copyright (c) 2008 Linux Foundation, written by Michael Kerrisk
2 .\" <mtk.manpages@gmail.com>
4 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
6 .TH PTHREAD_ATTR_INIT 3 2021-03-22 "Linux" "Linux Programmer's Manual"
8 pthread_attr_init, pthread_attr_destroy \- initialize and destroy
9 thread attributes object
12 .RI ( libpthread ", " \-lpthread )
15 .B #include <pthread.h>
17 .BI "int pthread_attr_init(pthread_attr_t *" attr );
18 .BI "int pthread_attr_destroy(pthread_attr_t *" attr );
22 .BR pthread_attr_init ()
23 function initializes the thread attributes object pointed to by
25 with default attribute values.
26 After this call, individual attributes of the object can be set
27 using various related functions (listed under SEE ALSO),
28 and then the object can be used in one or more
29 .BR pthread_create (3)
30 calls that create threads.
33 .BR pthread_attr_init ()
34 on a thread attributes object that has already been initialized
35 results in undefined behavior.
37 When a thread attributes object is no longer required,
38 it should be destroyed using the
39 .BR pthread_attr_destroy ()
41 Destroying a thread attributes object has no effect
42 on threads that were created using that object.
44 Once a thread attributes object has been destroyed,
45 it can be reinitialized using
46 .BR pthread_attr_init ().
47 Any other use of a destroyed thread attributes object
48 has undefined results.
50 On success, these functions return 0;
51 on error, they return a nonzero error number.
56 .BR pthread_attr_init ();
57 on Linux these functions always succeed
58 (but portable and future-proof applications should nevertheless
59 handle a possible error return).
61 For an explanation of the terms used in this section, see
69 Interface Attribute Value
71 .BR pthread_attr_init (),
72 .BR pthread_attr_destroy ()
73 T} Thread safety MT-Safe
79 POSIX.1-2001, POSIX.1-2008.
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
150 #define _GNU_SOURCE /* To get pthread_getattr_np() declaration */
157 #define handle_error_en(en, msg) \e
158 do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
161 display_pthread_attr(pthread_attr_t *attr, char *prefix)
166 struct sched_param sp;
168 s = pthread_attr_getdetachstate(attr, &i);
170 handle_error_en(s, "pthread_attr_getdetachstate");
171 printf("%sDetach state = %s\en", prefix,
172 (i == PTHREAD_CREATE_DETACHED) ? "PTHREAD_CREATE_DETACHED" :
173 (i == PTHREAD_CREATE_JOINABLE) ? "PTHREAD_CREATE_JOINABLE" :
176 s = pthread_attr_getscope(attr, &i);
178 handle_error_en(s, "pthread_attr_getscope");
179 printf("%sScope = %s\en", prefix,
180 (i == PTHREAD_SCOPE_SYSTEM) ? "PTHREAD_SCOPE_SYSTEM" :
181 (i == PTHREAD_SCOPE_PROCESS) ? "PTHREAD_SCOPE_PROCESS" :
184 s = pthread_attr_getinheritsched(attr, &i);
186 handle_error_en(s, "pthread_attr_getinheritsched");
187 printf("%sInherit scheduler = %s\en", prefix,
188 (i == PTHREAD_INHERIT_SCHED) ? "PTHREAD_INHERIT_SCHED" :
189 (i == PTHREAD_EXPLICIT_SCHED) ? "PTHREAD_EXPLICIT_SCHED" :
192 s = pthread_attr_getschedpolicy(attr, &i);
194 handle_error_en(s, "pthread_attr_getschedpolicy");
195 printf("%sScheduling policy = %s\en", prefix,
196 (i == SCHED_OTHER) ? "SCHED_OTHER" :
197 (i == SCHED_FIFO) ? "SCHED_FIFO" :
198 (i == SCHED_RR) ? "SCHED_RR" :
201 s = pthread_attr_getschedparam(attr, &sp);
203 handle_error_en(s, "pthread_attr_getschedparam");
204 printf("%sScheduling priority = %d\en", prefix, sp.sched_priority);
206 s = pthread_attr_getguardsize(attr, &v);
208 handle_error_en(s, "pthread_attr_getguardsize");
209 printf("%sGuard size = %zu bytes\en", prefix, v);
211 s = pthread_attr_getstack(attr, &stkaddr, &v);
213 handle_error_en(s, "pthread_attr_getstack");
214 printf("%sStack address = %p\en", prefix, stkaddr);
215 printf("%sStack size = %#zx bytes\en", prefix, v);
219 thread_start(void *arg)
222 pthread_attr_t gattr;
224 /* pthread_getattr_np() is a non\-standard GNU extension that
225 retrieves the attributes of the thread specified in its
228 s = pthread_getattr_np(pthread_self(), &gattr);
230 handle_error_en(s, "pthread_getattr_np");
232 printf("Thread attributes:\en");
233 display_pthread_attr(&gattr, "\et");
235 exit(EXIT_SUCCESS); /* Terminate all threads */
239 main(int argc, char *argv[])
243 pthread_attr_t *attrp; /* NULL or &attr */
248 /* If a command\-line argument was supplied, use it to set the
249 stack\-size attribute and set a few other thread attributes,
250 and set attrp pointing to thread attributes object. */
258 s = pthread_attr_init(&attr);
260 handle_error_en(s, "pthread_attr_init");
262 s = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
264 handle_error_en(s, "pthread_attr_setdetachstate");
266 s = pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
268 handle_error_en(s, "pthread_attr_setinheritsched");
270 stack_size = strtoul(argv[1], NULL, 0);
272 s = posix_memalign(&sp, sysconf(_SC_PAGESIZE), stack_size);
274 handle_error_en(s, "posix_memalign");
276 printf("posix_memalign() allocated at %p\en", sp);
278 s = pthread_attr_setstack(&attr, sp, stack_size);
280 handle_error_en(s, "pthread_attr_setstack");
283 s = pthread_create(&thr, attrp, &thread_start, NULL);
285 handle_error_en(s, "pthread_create");
288 s = pthread_attr_destroy(attrp);
290 handle_error_en(s, "pthread_attr_destroy");
293 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),