pthread_ge...default_np(3)
β¨ pthread_ge...default_np(3) β¨
okay bestie so you found a very niche corner of the Library Functions Manual (section 3, the library-calls girlies π
) and honestly? you ate. this page is about pthread_getattr_default_np and pthread_setattr_default_np, which β no cap β are about the default vibe your threads get born with πΌβ¨
πΈ NAME πΈ
pthread_getattr_default_np, pthread_setattr_default_np β get or set default thread-creation attributes π
(yes there is a lil trailing comma situation in the original name line, we don't talk about her π«Ά)
π LIBRARY π
POSIX threads library β that's libpthread, linked with -lpthread π§΅
π§ SYNOPSIS π§
#define _GNU_SOURCE /* See feature_test_macros(7) */
#include <pthread.h>
int pthread_getattr_default_np(pthread_attr_t *attr);
int pthread_setattr_default_np(const pthread_attr_t *attr);
so slay, we got two functions, both returning int, both living in <pthread.h>. and yeah, you need that _GNU_SOURCE define up top or she simply will not appear β¨
π DESCRIPTION π
so here's the tea, bestie β
pthread_setattr_default_np() sets the default attributes used when a brand new thread gets created β like, the attributes that get used when pthread_create(3) is called with a second argument that is NULL π«Ά. it's literally the "what happens if you don't specify anything" of thread creation.
you set those defaults using the attributes you hand over in *attr, which has to be a thread attributes object you already initialized. and now, the important details about that object you're passing in, lowkey:
- the attribute settings in the object must be valid β
- the stack address attribute must not be set in the object π
- setting the stack size attribute to zero means leave the default stack size unchanged π
meanwhile, pthread_getattr_default_np() initializes the thread attributes object pointed to by attr so it contains the default attributes used for thread creation. it's giving "show me the receipts" π
π ERRORS π
EINVAL(pthread_setattr_default_np()) β one of the attribute settings inattris invalid, or the stack address attribute is set inattr. nope, not today βENOMEM(pthread_setattr_default_np()) β insufficient memory. the memory said "no β€οΈ"
π ATTRIBUTES π
for an explanation of the terms used in this section, see attributes(7).
pthread_getattr_default_np(),pthread_setattr_default_np()β Thread safety: MT-Safe π¦
(the original puts this in a table, but tables are not the vibe here, so: MT-Safe, periodt)
π STANDARDS π
GNU. hence the suffix "_np" (nonportable) in their names β it's literally in the name that she doesn't travel well, iykyk π
π HISTORY π
glibc 2.18. a classic, honestly β¨
π EXAMPLES π
the program below uses pthread_getattr_default_np() to fetch the default thread-creation attributes and then displays various settings from the returned thread attributes object. when you run it, you see this:
$ ./a.out;
Stack size: 8388608
Guard size: 4096
Scheduling policy: SCHED_OTHER
Scheduling priority: 0
Detach state: JOINABLE
Inherit scheduler: INHERIT
π§ Program source π§
#define _GNU_SOURCE
#include <err.h>
#include <pthread.h>
#include <sched.h>
#include <stdio.h>
#include <stdlib.h>
static void
display_pthread_attr(pthread_attr_t *attr)
{
int s;
size_t stacksize;
size_t guardsize;
int policy;
struct sched_param schedparam;
int detachstate;
int inheritsched;
s = pthread_attr_getstacksize(attr, &stacksize);
if (s != 0)
errc(EXIT_FAILURE, s, "pthread_attr_getstacksize");
printf("Stack size: %zu\n", stacksize);
s = pthread_attr_getguardsize(attr, &guardsize);
if (s != 0)
errc(EXIT_FAILURE, s, "pthread_attr_getguardsize");
printf("Guard size: %zu\n", guardsize);
s = pthread_attr_getschedpolicy(attr, &policy);
if (s != 0)
errc(EXIT_FAILURE, s, "pthread_attr_getschedpolicy");
printf("Scheduling policy: %s\n",
(policy == SCHED_FIFO) ? "SCHED_FIFO" :
(policy == SCHED_RR) ? "SCHED_RR" :
(policy == SCHED_OTHER) ? "SCHED_OTHER" : "[unknown]");
s = pthread_attr_getschedparam(attr, &schedparam);
if (s != 0)
errc(EXIT_FAILURE, s, "pthread_attr_getschedparam");
printf("Scheduling priority: %d\n", schedparam.sched_priority);
s = pthread_attr_getdetachstate(attr, &detachstate);
if (s != 0)
errc(EXIT_FAILURE, s, "pthread_attr_getdetachstate");
printf("Detach state: %s\n",
(detachstate == PTHREAD_CREATE_DETACHED) ? "DETACHED" :
(detachstate == PTHREAD_CREATE_JOINABLE) ? "JOINABLE" :
"???");
s = pthread_attr_getinheritsched(attr, &inheritsched);
if (s != 0)
errc(EXIT_FAILURE, s, "pthread_attr_getinheritsched");
printf("Inherit scheduler: %s\n",
(inheritsched == PTHREAD_INHERIT_SCHED) ? "INHERIT" :
(inheritsched == PTHREAD_EXPLICIT_SCHED) ? "EXPLICIT" :
"???");
}
int
main(void)
{
int s;
pthread_attr_t attr;
s = pthread_getattr_default_np(&attr);
if (s != 0)
errc(EXIT_FAILURE, s, "pthread_getattr_default_np");
display_pthread_attr(&attr);
exit(EXIT_SUCCESS);
}
not me noticing every single error check in this example is errc(EXIT_FAILURE, ...) π it's called being thorough bestie, we love a defensive queen π«Ά
π SEE ALSO π
pthread_attr_getaffinity_np(3), pthread_attr_getdetachstate(3), pthread_attr_getguardsize(3), pthread_attr_getinheritsched(3), pthread_attr_getschedparam(3), pthread_attr_getschedpolicy(3), pthread_attr_getscope(3), pthread_attr_getstack(3), pthread_attr_getstackaddr(3), pthread_attr_getstacksize(3), pthread_attr_init(3), pthread_create(3), pthreads(7) π
and that's the whole page, bestie β¨ this one's from Linux man-pages 6.20, dated 2026-02-08, and it's giving very much "pthread_ge...default_np(3) but make it cute". go forth and set your defaults, you icon ππ¦π
π¦ where this page comes from
- arch rolling: man-pages 6.20-1 Β· BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages 6.20
- π the page's license: Linux-man-pages-copyleft
- Copyright 2016, Michael Kerrisk <mtk.manpages@gmail.com>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms