| 1 |
'\" t |
| 2 |
.\" Copyright 2016, Michael Kerrisk <[email protected]> |
| 3 |
.\" Copyright, the authors of the Linux man-pages project |
| 4 |
.\" |
| 5 |
.\" SPDX-License-Identifier: Linux-man-pages-copyleft |
| 6 |
.\" |
| 7 |
.TH pthread_getattr_default_np 3 2026-02-08 "Linux man-pages 6.18" |
| 8 |
.SH NAME |
| 9 |
pthread_getattr_default_np, pthread_setattr_default_np, \- |
| 10 |
get or set default thread-creation attributes |
| 11 |
.SH LIBRARY |
| 12 |
POSIX threads library |
| 13 |
.RI ( libpthread ,\~ \-lpthread ) |
| 14 |
.SH SYNOPSIS |
| 15 |
.nf |
| 16 |
.BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */" |
| 17 |
.B #include <pthread.h> |
| 18 |
.P |
| 19 |
.BI "int pthread_getattr_default_np(pthread_attr_t *" attr ); |
| 20 |
.BI "int pthread_setattr_default_np(const pthread_attr_t *" attr ); |
| 21 |
.fi |
| 22 |
.SH DESCRIPTION |
| 23 |
The |
| 24 |
.BR pthread_setattr_default_np () |
| 25 |
function sets the default attributes used for creation of a new |
| 26 |
thread\[em]that is, the attributes that are used when |
| 27 |
.BR pthread_create (3) |
| 28 |
is called with a second argument that is NULL. |
| 29 |
The default attributes are set using the attributes supplied in |
| 30 |
.IR *attr , |
| 31 |
a previously initialized thread attributes object. |
| 32 |
Note the following details about the supplied attributes object: |
| 33 |
.IP \[bu] 3 |
| 34 |
The attribute settings in the object must be valid. |
| 35 |
.IP \[bu] |
| 36 |
The |
| 37 |
.I stack address |
| 38 |
attribute must not be set in the object. |
| 39 |
.IP \[bu] |
| 40 |
Setting the |
| 41 |
.I stack size |
| 42 |
attribute to zero means leave the default stack size unchanged. |
| 43 |
.P |
| 44 |
The |
| 45 |
.BR pthread_getattr_default_np () |
| 46 |
function initializes the thread attributes object referred to by |
| 47 |
.I attr |
| 48 |
so that it contains the default attributes used for thread creation. |
| 49 |
.SH ERRORS |
| 50 |
.TP |
| 51 |
.B EINVAL |
| 52 |
.RB ( pthread_setattr_default_np ()) |
| 53 |
One of the attribute settings in |
| 54 |
.I attr |
| 55 |
is invalid, or the stack address attribute is set in |
| 56 |
.IR attr . |
| 57 |
.TP |
| 58 |
.B ENOMEM |
| 59 |
.\" Can happen (but unlikely) while trying to allocate memory for cpuset |
| 60 |
.RB ( pthread_setattr_default_np ()) |
| 61 |
Insufficient memory. |
| 62 |
.SH ATTRIBUTES |
| 63 |
For an explanation of the terms used in this section, see |
| 64 |
.BR attributes (7). |
| 65 |
.TS |
| 66 |
allbox; |
| 67 |
lbx lb lb |
| 68 |
l l l. |
| 69 |
Interface Attribute Value |
| 70 |
T{ |
| 71 |
.na |
| 72 |
.nh |
| 73 |
.BR pthread_getattr_default_np (), |
| 74 |
.BR pthread_setattr_default_np () |
| 75 |
T} Thread safety MT-Safe |
| 76 |
.TE |
| 77 |
.SH STANDARDS |
| 78 |
GNU; |
| 79 |
hence the suffix "_np" (nonportable) in their names. |
| 80 |
.SH HISTORY |
| 81 |
glibc 2.18. |
| 82 |
.SH EXAMPLES |
| 83 |
The program below uses |
| 84 |
.BR pthread_getattr_default_np () |
| 85 |
to fetch the default thread-creation attributes and then displays |
| 86 |
various settings from the returned thread attributes object. |
| 87 |
When running the program, we see the following output: |
| 88 |
.P |
| 89 |
.in +4n |
| 90 |
.EX |
| 91 |
.RB $ " ./a.out" ; |
| 92 |
Stack size: 8388608 |
| 93 |
Guard size: 4096 |
| 94 |
Scheduling policy: SCHED_OTHER |
| 95 |
Scheduling priority: 0 |
| 96 |
Detach state: JOINABLE |
| 97 |
Inherit scheduler: INHERIT |
| 98 |
.EE |
| 99 |
.in |
| 100 |
.SS Program source |
| 101 |
\& |
| 102 |
.\" SRC BEGIN (pthread_getattr_default_np.c) |
| 103 |
.EX |
| 104 |
#define _GNU_SOURCE |
| 105 |
#include <err.h> |
| 106 |
#include <pthread.h> |
| 107 |
#include <sched.h> |
| 108 |
#include <stdio.h> |
| 109 |
#include <stdlib.h> |
| 110 |
\& |
| 111 |
static void |
| 112 |
display_pthread_attr(pthread_attr_t *attr) |
| 113 |
{ |
| 114 |
int s; |
| 115 |
size_t stacksize; |
| 116 |
size_t guardsize; |
| 117 |
int policy; |
| 118 |
struct sched_param schedparam; |
| 119 |
int detachstate; |
| 120 |
int inheritsched; |
| 121 |
\& |
| 122 |
s = pthread_attr_getstacksize(attr, &stacksize); |
| 123 |
if (s != 0) |
| 124 |
errc(EXIT_FAILURE, s, "pthread_attr_getstacksize"); |
| 125 |
printf("Stack size: %zu\[rs]n", stacksize); |
| 126 |
\& |
| 127 |
s = pthread_attr_getguardsize(attr, &guardsize); |
| 128 |
if (s != 0) |
| 129 |
errc(EXIT_FAILURE, s, "pthread_attr_getguardsize"); |
| 130 |
printf("Guard size: %zu\[rs]n", guardsize); |
| 131 |
\& |
| 132 |
s = pthread_attr_getschedpolicy(attr, &policy); |
| 133 |
if (s != 0) |
| 134 |
errc(EXIT_FAILURE, s, "pthread_attr_getschedpolicy"); |
| 135 |
printf("Scheduling policy: %s\[rs]n", |
| 136 |
(policy == SCHED_FIFO) ? "SCHED_FIFO" : |
| 137 |
(policy == SCHED_RR) ? "SCHED_RR" : |
| 138 |
(policy == SCHED_OTHER) ? "SCHED_OTHER" : "[unknown]"); |
| 139 |
\& |
| 140 |
s = pthread_attr_getschedparam(attr, &schedparam); |
| 141 |
if (s != 0) |
| 142 |
errc(EXIT_FAILURE, s, "pthread_attr_getschedparam"); |
| 143 |
printf("Scheduling priority: %d\[rs]n", schedparam.sched_priority); |
| 144 |
\& |
| 145 |
s = pthread_attr_getdetachstate(attr, &detachstate); |
| 146 |
if (s != 0) |
| 147 |
errc(EXIT_FAILURE, s, "pthread_attr_getdetachstate"); |
| 148 |
printf("Detach state: %s\[rs]n", |
| 149 |
(detachstate == PTHREAD_CREATE_DETACHED) ? "DETACHED" : |
| 150 |
(detachstate == PTHREAD_CREATE_JOINABLE) ? "JOINABLE" : |
| 151 |
"???"); |
| 152 |
\& |
| 153 |
s = pthread_attr_getinheritsched(attr, &inheritsched); |
| 154 |
if (s != 0) |
| 155 |
errc(EXIT_FAILURE, s, "pthread_attr_getinheritsched"); |
| 156 |
printf("Inherit scheduler: %s\[rs]n", |
| 157 |
(inheritsched == PTHREAD_INHERIT_SCHED) ? "INHERIT" : |
| 158 |
(inheritsched == PTHREAD_EXPLICIT_SCHED) ? "EXPLICIT" : |
| 159 |
"???"); |
| 160 |
} |
| 161 |
\& |
| 162 |
int |
| 163 |
main(void) |
| 164 |
{ |
| 165 |
int s; |
| 166 |
pthread_attr_t attr; |
| 167 |
\& |
| 168 |
s = pthread_getattr_default_np(&attr); |
| 169 |
if (s != 0) |
| 170 |
errc(EXIT_FAILURE, s, "pthread_getattr_default_np"); |
| 171 |
\& |
| 172 |
display_pthread_attr(&attr); |
| 173 |
\& |
| 174 |
exit(EXIT_SUCCESS); |
| 175 |
} |
| 176 |
.EE |
| 177 |
.\" SRC END |
| 178 |
.SH SEE ALSO |
| 179 |
.ad l |
| 180 |
.nh |
| 181 |
.BR pthread_attr_getaffinity_np (3), |
| 182 |
.BR pthread_attr_getdetachstate (3), |
| 183 |
.BR pthread_attr_getguardsize (3), |
| 184 |
.BR pthread_attr_getinheritsched (3), |
| 185 |
.BR pthread_attr_getschedparam (3), |
| 186 |
.BR pthread_attr_getschedpolicy (3), |
| 187 |
.BR pthread_attr_getscope (3), |
| 188 |
.BR pthread_attr_getstack (3), |
| 189 |
.BR pthread_attr_getstackaddr (3), |
| 190 |
.BR pthread_attr_getstacksize (3), |
| 191 |
.BR pthread_attr_init (3), |
| 192 |
.BR pthread_create (3), |
| 193 |
.BR pthreads (7) |