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(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
34 attributes object referred to by attr so that it contains the default
35 attributes 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
48 attributes(7).
49
50 ┌───────────────────────────────┬───────────────┬─────────┐
51 │Interface │ Attribute │ Value │
52 ├───────────────────────────────┼───────────────┼─────────┤
53 │pthread_getattr_default_np(), │ Thread safety │ MT-Safe │
54 │pthread_setattr_default_np() │ │ │
55 └───────────────────────────────┴───────────────┴─────────┘
57 These functions are nonstandard GNU extensions; hence the suffix "_np"
58 (nonportable) in their names.
59
61 The program below uses pthread_getattr_default_np() to fetch the
62 default thread-creation attributes and then displays various settings
63 from the returned thread attributes object. When running the program,
64 we see the following output:
65
66 $ ./a.out
67 Stack size: 8388608
68 Guard size: 4096
69 Scheduling policy: SCHED_OTHER
70 Scheduling priority: 0
71 Detach state: JOINABLE
72 Inherit scheduler: INHERIT
73
74 Program source
75
76 #define _GNU_SOURCE
77 #include <pthread.h>
78 #include <stdio.h>
79 #include <stdlib.h>
80 #include <errno.h>
81
82 #define errExitEN(en, msg) \
83 do { errno = en; perror(msg); \
84 exit(EXIT_FAILURE); } while (0)
85
86 static void
87 display_pthread_attr(pthread_attr_t *attr)
88 {
89 int s;
90 size_t stacksize;
91 size_t guardsize;
92 int policy;
93 struct sched_param schedparam;
94 int detachstate;
95 int inheritsched;
96
97 s = pthread_attr_getstacksize(attr, &stacksize);
98 if (s != 0)
99 errExitEN(s, "pthread_attr_getstacksize");
100 printf("Stack size: %zd\n", stacksize);
101
102 s = pthread_attr_getguardsize(attr, &guardsize);
103 if (s != 0)
104 errExitEN(s, "pthread_attr_getguardsize");
105 printf("Guard size: %zd\n", guardsize);
106
107 s = pthread_attr_getschedpolicy(attr, &policy);
108 if (s != 0)
109 errExitEN(s, "pthread_attr_getschedpolicy");
110 printf("Scheduling policy: %s\n",
111 (policy == SCHED_FIFO) ? "SCHED_FIFO" :
112 (policy == SCHED_RR) ? "SCHED_RR" :
113 (policy == SCHED_OTHER) ? "SCHED_OTHER" : "[unknown]");
114
115 s = pthread_attr_getschedparam(attr, &schedparam);
116 if (s != 0)
117 errExitEN(s, "pthread_attr_getschedparam");
118 printf("Scheduling priority: %d\n", schedparam.sched_priority);
119
120 s = pthread_attr_getdetachstate(attr, &detachstate);
121 if (s != 0)
122 errExitEN(s, "pthread_attr_getdetachstate");
123 printf("Detach state: %s\n",
124 (detachstate == PTHREAD_CREATE_DETACHED) ? "DETACHED" :
125 (detachstate == PTHREAD_CREATE_JOINABLE) ? "JOINABLE" :
126 "???");
127
128 s = pthread_attr_getinheritsched(attr, &inheritsched);
129 if (s != 0)
130 errExitEN(s, "pthread_attr_getinheritsched");
131 printf("Inherit scheduler: %s\n",
132 (inheritsched == PTHREAD_INHERIT_SCHED) ? "INHERIT" :
133 (inheritsched == PTHREAD_EXPLICIT_SCHED) ? "EXPLICIT" :
134 "???");
135 }
136
137 int
138 main(int argc, char *argv[])
139 {
140 int s;
141 pthread_attr_t attr;
142
143 s = pthread_getattr_default_np(&attr);
144 if (s != 0)
145 errExitEN(s, "pthread_getattr_default_np");
146
147 display_pthread_attr(&attr);
148
149 exit(EXIT_SUCCESS);
150 }
151
153 pthread_attr_getaffinity_np(3), pthread_attr_getdetachstate(3),
154 pthread_attr_getguardsize(3), pthread_attr_getinheritsched(3),
155 pthread_attr_getschedparam(3), pthread_attr_getschedpolicy(3),
156 pthread_attr_getscope(3), pthread_attr_getstack(3),
157 pthread_attr_getstackaddr(3), pthread_attr_getstacksize(3),
158 pthread_attr_init(3), pthread_create(3), pthreads(7)
159
161 This page is part of release 5.07 of the Linux man-pages project. A
162 description of the project, information about reporting bugs, and the
163 latest version of this page, can be found at
164 https://www.kernel.org/doc/man-pages/.
165
166
167
168Linux 2020-06-09 PTHREAD_GETATTR_DEFAULT_NP(3)