1 .\" Copyright (c) 2016 Michael Kerrisk <mtk.manpages@gmail.com>
3 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
5 .TH PTHREAD_GETATTR_DEFAULT_NP 3 2021-03-22 "Linux" "Linux Programmer's Manual"
7 pthread_getattr_default_np, pthread_setattr_default_np, \-
8 get or set default thread-creation attributes
11 .RI ( libpthread ", " \-lpthread )
14 .BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */"
15 .B #include <pthread.h>
17 .BI "int pthread_getattr_default_np(pthread_attr_t *" attr );
18 .BI "int pthread_setattr_default_np(const pthread_attr_t *" attr );
22 .BR pthread_setattr_default_np ()
23 function sets the default attributes used for creation of a new
24 thread\(emthat is, the attributes that are used when
25 .BR pthread_create (3)
26 is called with a second argument that is NULL.
27 The default attributes are set using the attributes supplied in
29 a previously initialized thread attributes object.
30 Note the following details about the supplied attributes object:
32 The attribute settings in the object must be valid.
36 attribute must not be set in the object.
40 attribute to zero means leave the default stack size unchanged.
43 .BR pthread_getattr_default_np ()
44 function initializes the thread attributes object referred to by
46 so that it contains the default attributes used for thread creation.
50 .RB ( pthread_setattr_default_np ())
51 One of the attribute settings in
53 is invalid, or the stack address attribute is set in
57 .\" Can happen (but unlikely) while trying to allocate memory for cpuset
58 .RB ( pthread_setattr_default_np ())
61 These functions are available in glibc since version 2.18.
63 For an explanation of the terms used in this section, see
71 Interface Attribute Value
73 .BR pthread_getattr_default_np (),
74 .BR pthread_setattr_default_np ()
75 T} Thread safety MT-Safe
81 These functions are nonstandard GNU extensions;
82 hence the suffix "_np" (nonportable) in their names.
84 The program below uses
85 .BR pthread_getattr_default_np ()
86 to fetch the default thread-creation attributes and then displays
87 various settings from the returned thread attributes object.
88 When running the program, we see the following output:
95 Scheduling policy: SCHED_OTHER
96 Scheduling priority: 0
97 Detach state: JOINABLE
98 Inherit scheduler: INHERIT
110 #define errExitEN(en, msg) \e
111 do { errno = en; perror(msg); \e
112 exit(EXIT_FAILURE); } while (0)
115 display_pthread_attr(pthread_attr_t *attr)
121 struct sched_param schedparam;
125 s = pthread_attr_getstacksize(attr, &stacksize);
127 errExitEN(s, "pthread_attr_getstacksize");
128 printf("Stack size: %zd\en", stacksize);
130 s = pthread_attr_getguardsize(attr, &guardsize);
132 errExitEN(s, "pthread_attr_getguardsize");
133 printf("Guard size: %zd\en", guardsize);
135 s = pthread_attr_getschedpolicy(attr, &policy);
137 errExitEN(s, "pthread_attr_getschedpolicy");
138 printf("Scheduling policy: %s\en",
139 (policy == SCHED_FIFO) ? "SCHED_FIFO" :
140 (policy == SCHED_RR) ? "SCHED_RR" :
141 (policy == SCHED_OTHER) ? "SCHED_OTHER" : "[unknown]");
143 s = pthread_attr_getschedparam(attr, &schedparam);
145 errExitEN(s, "pthread_attr_getschedparam");
146 printf("Scheduling priority: %d\en", schedparam.sched_priority);
148 s = pthread_attr_getdetachstate(attr, &detachstate);
150 errExitEN(s, "pthread_attr_getdetachstate");
151 printf("Detach state: %s\en",
152 (detachstate == PTHREAD_CREATE_DETACHED) ? "DETACHED" :
153 (detachstate == PTHREAD_CREATE_JOINABLE) ? "JOINABLE" :
156 s = pthread_attr_getinheritsched(attr, &inheritsched);
158 errExitEN(s, "pthread_attr_getinheritsched");
159 printf("Inherit scheduler: %s\en",
160 (inheritsched == PTHREAD_INHERIT_SCHED) ? "INHERIT" :
161 (inheritsched == PTHREAD_EXPLICIT_SCHED) ? "EXPLICIT" :
166 main(int argc, char *argv[])
171 s = pthread_getattr_default_np(&attr);
173 errExitEN(s, "pthread_getattr_default_np");
175 display_pthread_attr(&attr);
183 .BR pthread_attr_getaffinity_np (3),
184 .BR pthread_attr_getdetachstate (3),
185 .BR pthread_attr_getguardsize (3),
186 .BR pthread_attr_getinheritsched (3),
187 .BR pthread_attr_getschedparam (3),
188 .BR pthread_attr_getschedpolicy (3),
189 .BR pthread_attr_getscope (3),
190 .BR pthread_attr_getstack (3),
191 .BR pthread_attr_getstackaddr (3),
192 .BR pthread_attr_getstacksize (3),
193 .BR pthread_attr_init (3),
194 .BR pthread_create (3),