1pthread_getattr_default_np(3L)ibrary Functions Manuaplthread_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 POSIX threads library (libpthread, -lpthread)
11
13 #define _GNU_SOURCE /* See feature_test_macros(7) */
14 #include <pthread.h>
15
16 int pthread_getattr_default_np(pthread_attr_t *attr);
17 int pthread_setattr_default_np(const pthread_attr_t *attr);
18
20 The pthread_setattr_default_np() function sets the default attributes
21 used for creation of a new thread—that is, the attributes that are used
22 when pthread_create(3) is called with a second argument that is NULL.
23 The default attributes are set using the attributes supplied in *attr,
24 a previously initialized thread attributes object. Note the following
25 details about the supplied attributes object:
26
27 • The attribute settings in the object must be valid.
28
29 • The stack address attribute must not be set in the object.
30
31 • Setting the stack size attribute to zero means leave the default
32 stack size unchanged.
33
34 The pthread_getattr_default_np() function initializes the thread at‐
35 tributes object referred to by attr so that it contains the default at‐
36 tributes used for thread creation.
37
39 EINVAL (pthread_setattr_default_np()) One of the attribute settings in
40 attr is invalid, or the stack address attribute is set in attr.
41
42 ENOMEM (pthread_setattr_default_np()) Insufficient memory.
43
45 For an explanation of the terms used in this section, see at‐
46 tributes(7).
47
48 ┌────────────────────────────────────────────┬───────────────┬─────────┐
49 │Interface │ Attribute │ Value │
50 ├────────────────────────────────────────────┼───────────────┼─────────┤
51 │pthread_getattr_default_np(), │ Thread safety │ MT-Safe │
52 │pthread_setattr_default_np() │ │ │
53 └────────────────────────────────────────────┴───────────────┴─────────┘
54
56 GNU; hence the suffix "_np" (nonportable) in their names.
57
59 glibc 2.18.
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 <err.h>
79 #include <errno.h>
80 #include <pthread.h>
81 #include <stdio.h>
82 #include <stdlib.h>
83
84 static void
85 display_pthread_attr(pthread_attr_t *attr)
86 {
87 int s;
88 size_t stacksize;
89 size_t guardsize;
90 int policy;
91 struct sched_param schedparam;
92 int detachstate;
93 int inheritsched;
94
95 s = pthread_attr_getstacksize(attr, &stacksize);
96 if (s != 0)
97 errc(EXIT_FAILURE, s, "pthread_attr_getstacksize");
98 printf("Stack size: %zd\n", stacksize);
99
100 s = pthread_attr_getguardsize(attr, &guardsize);
101 if (s != 0)
102 errc(EXIT_FAILURE, s, "pthread_attr_getguardsize");
103 printf("Guard size: %zd\n", guardsize);
104
105 s = pthread_attr_getschedpolicy(attr, &policy);
106 if (s != 0)
107 errc(EXIT_FAILURE, s, "pthread_attr_getschedpolicy");
108 printf("Scheduling policy: %s\n",
109 (policy == SCHED_FIFO) ? "SCHED_FIFO" :
110 (policy == SCHED_RR) ? "SCHED_RR" :
111 (policy == SCHED_OTHER) ? "SCHED_OTHER" : "[unknown]");
112
113 s = pthread_attr_getschedparam(attr, &schedparam);
114 if (s != 0)
115 errc(EXIT_FAILURE, s, "pthread_attr_getschedparam");
116 printf("Scheduling priority: %d\n", schedparam.sched_priority);
117
118 s = pthread_attr_getdetachstate(attr, &detachstate);
119 if (s != 0)
120 errc(EXIT_FAILURE, s, "pthread_attr_getdetachstate");
121 printf("Detach state: %s\n",
122 (detachstate == PTHREAD_CREATE_DETACHED) ? "DETACHED" :
123 (detachstate == PTHREAD_CREATE_JOINABLE) ? "JOINABLE" :
124 "???");
125
126 s = pthread_attr_getinheritsched(attr, &inheritsched);
127 if (s != 0)
128 errc(EXIT_FAILURE, s, "pthread_attr_getinheritsched");
129 printf("Inherit scheduler: %s\n",
130 (inheritsched == PTHREAD_INHERIT_SCHED) ? "INHERIT" :
131 (inheritsched == PTHREAD_EXPLICIT_SCHED) ? "EXPLICIT" :
132 "???");
133 }
134
135 int
136 main(void)
137 {
138 int s;
139 pthread_attr_t attr;
140
141 s = pthread_getattr_default_np(&attr);
142 if (s != 0)
143 errc(EXIT_FAILURE, s, "pthread_getattr_default_np");
144
145 display_pthread_attr(&attr);
146
147 exit(EXIT_SUCCESS);
148 }
149
151 pthread_attr_getaffinity_np(3), pthread_attr_getdetachstate(3),
152 pthread_attr_getguardsize(3), pthread_attr_getinheritsched(3),
153 pthread_attr_getschedparam(3), pthread_attr_getschedpolicy(3),
154 pthread_attr_getscope(3), pthread_attr_getstack(3),
155 pthread_attr_getstackaddr(3), pthread_attr_getstacksize(3),
156 pthread_attr_init(3), pthread_create(3), pthreads(7)
157
158
159
160Linux man-pages 6.04 2023-03-30 pthread_getattr_default_np(3)