1PTHREAD_GETATTR_DEFAULT_NP(3L)inux Programmer's ManuPaTlHREAD_GETATTR_DEFAULT_NP(3)
2
3
4
6 pthread_getattr_default_np, pthread_setattr_default_np, - get or set
7 default thread-creation attributes
8
10 #define _GNU_SOURCE /* See feature_test_macros(7) */
11 #include <pthread.h>
12
13 int pthread_getattr_default_np(pthread_attr_t *attr);
14 int pthread_setattr_default_np(const pthread_attr_t *attr);
15
16 Compile and link with -pthread.
17
19 The pthread_setattr_default_np() function sets the default attributes
20 used for creation of a new thread—that is, the attributes that are used
21 when pthread_create(3) is called with a second argument that is NULL.
22 The default attributes are set using the attributes supplied in *attr,
23 a previously initialized thread attributes object. Note the following
24 details about the supplied attributes object:
25
26 * The attribute settings in the object must be valid.
27
28 * The stack address attribute must not be set in the object.
29
30 * Setting the stack size attribute to zero means leave the default
31 stack size unchanged.
32
33 The pthread_getattr_default_np() function initializes the thread at‐
34 tributes object referred to by attr so that it contains the default at‐
35 tributes used for thread creation.
36
38 EINVAL (pthread_setattr_default_np()) One of the attribute settings in
39 attr is invalid, or the stack address attribute is set in attr.
40
41 ENOMEM (pthread_setattr_default_np()) Insufficient memory.
42
44 These functions are available in glibc since version 2.18.
45
47 For an explanation of the terms used in this section, see at‐
48 tributes(7).
49
50 ┌────────────────────────────────────────────┬───────────────┬─────────┐
51 │Interface │ Attribute │ Value │
52 ├────────────────────────────────────────────┼───────────────┼─────────┤
53 │pthread_getattr_default_np(), │ Thread safety │ MT-Safe │
54 │pthread_setattr_default_np() │ │ │
55 └────────────────────────────────────────────┴───────────────┴─────────┘
56
58 These functions are nonstandard GNU extensions; hence the suffix "_np"
59 (nonportable) in their names.
60
62 The program below uses pthread_getattr_default_np() to fetch the de‐
63 fault thread-creation attributes and then displays various settings
64 from the returned thread attributes object. When running the program,
65 we see the following output:
66
67 $ ./a.out
68 Stack size: 8388608
69 Guard size: 4096
70 Scheduling policy: SCHED_OTHER
71 Scheduling priority: 0
72 Detach state: JOINABLE
73 Inherit scheduler: INHERIT
74
75 Program source
76
77 #define _GNU_SOURCE
78 #include <pthread.h>
79 #include <stdio.h>
80 #include <stdlib.h>
81 #include <errno.h>
82
83 #define errExitEN(en, msg) \
84 do { errno = en; perror(msg); \
85 exit(EXIT_FAILURE); } while (0)
86
87 static void
88 display_pthread_attr(pthread_attr_t *attr)
89 {
90 int s;
91 size_t stacksize;
92 size_t guardsize;
93 int policy;
94 struct sched_param schedparam;
95 int detachstate;
96 int inheritsched;
97
98 s = pthread_attr_getstacksize(attr, &stacksize);
99 if (s != 0)
100 errExitEN(s, "pthread_attr_getstacksize");
101 printf("Stack size: %zd\n", stacksize);
102
103 s = pthread_attr_getguardsize(attr, &guardsize);
104 if (s != 0)
105 errExitEN(s, "pthread_attr_getguardsize");
106 printf("Guard size: %zd\n", guardsize);
107
108 s = pthread_attr_getschedpolicy(attr, &policy);
109 if (s != 0)
110 errExitEN(s, "pthread_attr_getschedpolicy");
111 printf("Scheduling policy: %s\n",
112 (policy == SCHED_FIFO) ? "SCHED_FIFO" :
113 (policy == SCHED_RR) ? "SCHED_RR" :
114 (policy == SCHED_OTHER) ? "SCHED_OTHER" : "[unknown]");
115
116 s = pthread_attr_getschedparam(attr, &schedparam);
117 if (s != 0)
118 errExitEN(s, "pthread_attr_getschedparam");
119 printf("Scheduling priority: %d\n", schedparam.sched_priority);
120
121 s = pthread_attr_getdetachstate(attr, &detachstate);
122 if (s != 0)
123 errExitEN(s, "pthread_attr_getdetachstate");
124 printf("Detach state: %s\n",
125 (detachstate == PTHREAD_CREATE_DETACHED) ? "DETACHED" :
126 (detachstate == PTHREAD_CREATE_JOINABLE) ? "JOINABLE" :
127 "???");
128
129 s = pthread_attr_getinheritsched(attr, &inheritsched);
130 if (s != 0)
131 errExitEN(s, "pthread_attr_getinheritsched");
132 printf("Inherit scheduler: %s\n",
133 (inheritsched == PTHREAD_INHERIT_SCHED) ? "INHERIT" :
134 (inheritsched == PTHREAD_EXPLICIT_SCHED) ? "EXPLICIT" :
135 "???");
136 }
137
138 int
139 main(int argc, char *argv[])
140 {
141 int s;
142 pthread_attr_t attr;
143
144 s = pthread_getattr_default_np(&attr);
145 if (s != 0)
146 errExitEN(s, "pthread_getattr_default_np");
147
148 display_pthread_attr(&attr);
149
150 exit(EXIT_SUCCESS);
151 }
152
154 pthread_attr_getaffinity_np(3), pthread_attr_getdetachstate(3),
155 pthread_attr_getguardsize(3), pthread_attr_getinheritsched(3),
156 pthread_attr_getschedparam(3), pthread_attr_getschedpolicy(3),
157 pthread_attr_getscope(3), pthread_attr_getstack(3),
158 pthread_attr_getstackaddr(3), pthread_attr_getstacksize(3),
159 pthread_attr_init(3), pthread_create(3), pthreads(7)
160
162 This page is part of release 5.13 of the Linux man-pages project. A
163 description of the project, information about reporting bugs, and the
164 latest version of this page, can be found at
165 https://www.kernel.org/doc/man-pages/.
166
167
168
169Linux 2021-03-22 PTHREAD_GETATTR_DEFAULT_NP(3)