1 .\" Copyright (c) 2008 Linux Foundation, written by Michael Kerrisk
2 .\" <mtk.manpages@gmail.com>
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.
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.
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
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
26 .TH PTHREAD_ATTR_INIT 3 2021-03-22 "Linux" "Linux Programmer's Manual"
28 pthread_attr_init, pthread_attr_destroy \- initialize and destroy
29 thread attributes object
32 .B #include <pthread.h>
34 .BI "int pthread_attr_init(pthread_attr_t *" attr );
35 .BI "int pthread_attr_destroy(pthread_attr_t *" attr );
37 Compile and link with \fI\-pthread\fP.
41 .BR pthread_attr_init ()
42 function initializes the thread attributes object pointed to by
44 with default attribute values.
45 After this call, individual attributes of the object can be set
46 using various related functions (listed under SEE ALSO),
47 and then the object can be used in one or more
48 .BR pthread_create (3)
49 calls that create threads.
52 .BR pthread_attr_init ()
53 on a thread attributes object that has already been initialized
54 results in undefined behavior.
56 When a thread attributes object is no longer required,
57 it should be destroyed using the
58 .BR pthread_attr_destroy ()
60 Destroying a thread attributes object has no effect
61 on threads that were created using that object.
63 Once a thread attributes object has been destroyed,
64 it can be reinitialized using
65 .BR pthread_attr_init ().
66 Any other use of a destroyed thread attributes object
67 has undefined results.
69 On success, these functions return 0;
70 on error, they return a nonzero error number.
75 .BR pthread_attr_init ();
76 on Linux these functions always succeed
77 (but portable and future-proof applications should nevertheless
78 handle a possible error return).
80 For an explanation of the terms used in this section, see
88 Interface Attribute Value
90 .BR pthread_attr_init (),
91 .BR pthread_attr_destroy ()
92 T} Thread safety MT-Safe
98 POSIX.1-2001, POSIX.1-2008.
102 type should be treated as opaque:
103 any access to the object other than via pthreads functions
104 is nonportable and produces undefined results.
106 The program below optionally makes use of
107 .BR pthread_attr_init ()
108 and various related functions to initialize a thread attributes
109 object that is used to create a single thread.
110 Once created, the thread uses the
111 .BR pthread_getattr_np (3)
112 function (a nonstandard GNU extension) to retrieve the thread's
113 attributes, and then displays those attributes.
115 If the program is run with no command-line argument,
116 then it passes NULL as the
119 .BR pthread_create (3),
120 so that the thread is created with default attributes.
121 Running the program on Linux/x86-32 with the NPTL threading implementation,
122 we see the following:
126 .\" Results from glibc 2.8, SUSE 11.0; Oct 2008
127 .RB "$" " ulimit \-s" " # No stack limit ==> default stack size is 2 MB"
131 Detach state = PTHREAD_CREATE_JOINABLE
132 Scope = PTHREAD_SCOPE_SYSTEM
133 Inherit scheduler = PTHREAD_INHERIT_SCHED
134 Scheduling policy = SCHED_OTHER
135 Scheduling priority = 0
136 Guard size = 4096 bytes
137 Stack address = 0x40196000
138 Stack size = 0x201000 bytes
142 When we supply a stack size as a command-line argument,
143 the program initializes a thread attributes object,
144 sets various attributes in that object,
145 and passes a pointer to the object in the call to
146 .BR pthread_create (3).
147 Running the program on Linux/x86-32 with the NPTL threading implementation,
148 we see the following:
152 .\" Results from glibc 2.8, SUSE 11.0; Oct 2008
153 .RB "$" " ./a.out 0x3000000"
154 posix_memalign() allocated at 0x40197000
156 Detach state = PTHREAD_CREATE_DETACHED
157 Scope = PTHREAD_SCOPE_SYSTEM
158 Inherit scheduler = PTHREAD_EXPLICIT_SCHED
159 Scheduling policy = SCHED_OTHER
160 Scheduling priority = 0
162 Stack address = 0x40197000
163 Stack size = 0x3000000 bytes
169 #define _GNU_SOURCE /* To get pthread_getattr_np() declaration */
176 #define handle_error_en(en, msg) \e
177 do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
180 display_pthread_attr(pthread_attr_t *attr, char *prefix)
185 struct sched_param sp;
187 s = pthread_attr_getdetachstate(attr, &i);
189 handle_error_en(s, "pthread_attr_getdetachstate");
190 printf("%sDetach state = %s\en", prefix,
191 (i == PTHREAD_CREATE_DETACHED) ? "PTHREAD_CREATE_DETACHED" :
192 (i == PTHREAD_CREATE_JOINABLE) ? "PTHREAD_CREATE_JOINABLE" :
195 s = pthread_attr_getscope(attr, &i);
197 handle_error_en(s, "pthread_attr_getscope");
198 printf("%sScope = %s\en", prefix,
199 (i == PTHREAD_SCOPE_SYSTEM) ? "PTHREAD_SCOPE_SYSTEM" :
200 (i == PTHREAD_SCOPE_PROCESS) ? "PTHREAD_SCOPE_PROCESS" :
203 s = pthread_attr_getinheritsched(attr, &i);
205 handle_error_en(s, "pthread_attr_getinheritsched");
206 printf("%sInherit scheduler = %s\en", prefix,
207 (i == PTHREAD_INHERIT_SCHED) ? "PTHREAD_INHERIT_SCHED" :
208 (i == PTHREAD_EXPLICIT_SCHED) ? "PTHREAD_EXPLICIT_SCHED" :
211 s = pthread_attr_getschedpolicy(attr, &i);
213 handle_error_en(s, "pthread_attr_getschedpolicy");
214 printf("%sScheduling policy = %s\en", prefix,
215 (i == SCHED_OTHER) ? "SCHED_OTHER" :
216 (i == SCHED_FIFO) ? "SCHED_FIFO" :
217 (i == SCHED_RR) ? "SCHED_RR" :
220 s = pthread_attr_getschedparam(attr, &sp);
222 handle_error_en(s, "pthread_attr_getschedparam");
223 printf("%sScheduling priority = %d\en", prefix, sp.sched_priority);
225 s = pthread_attr_getguardsize(attr, &v);
227 handle_error_en(s, "pthread_attr_getguardsize");
228 printf("%sGuard size = %zu bytes\en", prefix, v);
230 s = pthread_attr_getstack(attr, &stkaddr, &v);
232 handle_error_en(s, "pthread_attr_getstack");
233 printf("%sStack address = %p\en", prefix, stkaddr);
234 printf("%sStack size = %#zx bytes\en", prefix, v);
238 thread_start(void *arg)
241 pthread_attr_t gattr;
243 /* pthread_getattr_np() is a non\-standard GNU extension that
244 retrieves the attributes of the thread specified in its
247 s = pthread_getattr_np(pthread_self(), &gattr);
249 handle_error_en(s, "pthread_getattr_np");
251 printf("Thread attributes:\en");
252 display_pthread_attr(&gattr, "\et");
254 exit(EXIT_SUCCESS); /* Terminate all threads */
258 main(int argc, char *argv[])
262 pthread_attr_t *attrp; /* NULL or &attr */
267 /* If a command\-line argument was supplied, use it to set the
268 stack\-size attribute and set a few other thread attributes,
269 and set attrp pointing to thread attributes object. */
277 s = pthread_attr_init(&attr);
279 handle_error_en(s, "pthread_attr_init");
281 s = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
283 handle_error_en(s, "pthread_attr_setdetachstate");
285 s = pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
287 handle_error_en(s, "pthread_attr_setinheritsched");
289 stack_size = strtoul(argv[1], NULL, 0);
291 s = posix_memalign(&sp, sysconf(_SC_PAGESIZE), stack_size);
293 handle_error_en(s, "posix_memalign");
295 printf("posix_memalign() allocated at %p\en", sp);
297 s = pthread_attr_setstack(&attr, sp, stack_size);
299 handle_error_en(s, "pthread_attr_setstack");
302 s = pthread_create(&thr, attrp, &thread_start, NULL);
304 handle_error_en(s, "pthread_create");
307 s = pthread_attr_destroy(attrp);
309 handle_error_en(s, "pthread_attr_destroy");
312 pause(); /* Terminates when other thread calls exit() */
318 .BR pthread_attr_setaffinity_np (3),
319 .BR pthread_attr_setdetachstate (3),
320 .BR pthread_attr_setguardsize (3),
321 .BR pthread_attr_setinheritsched (3),
322 .BR pthread_attr_setschedparam (3),
323 .BR pthread_attr_setschedpolicy (3),
324 .BR pthread_attr_setscope (3),
325 .BR pthread_attr_setsigmask_np (3),
326 .BR pthread_attr_setstack (3),
327 .BR pthread_attr_setstackaddr (3),
328 .BR pthread_attr_setstacksize (3),
329 .BR pthread_create (3),
330 .BR pthread_getattr_np (3),
331 .BR pthread_setattr_default_np (3),