PTHREAD_ATTR(3)
π PTHREAD_ATTR(3) β thread attribute operations, bestie edition β¨
Ok so this is the pthread_attr page from the Library Functions Manual (section 3, iykyk) and it's literally the whole glow-up kit for threads. Gonna walk you through every single one, no skips, no cap. π«Ά
πΈ LIBRARY
POSIX Threads Library, aka libpthread, aka -lpthread when you're linking. She's iconic, she's classic. π
β¨ SYNOPSIS β¨
First things first, the include:
#include <pthread.h>
Then the whole squad, one by one π :
int
pthread_attr_init(pthread_attr_t *attr);
int
pthread_attr_destroy(pthread_attr_t *attr);
int
pthread_attr_setstack(pthread_attr_t *attr, void *stackaddr,
size_t stacksize);
int
pthread_attr_getstack(const pthread_attr_t * restrict attr,
void ** restrict stackaddr, size_t * restrict stacksize);
int
pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize);
int
pthread_attr_getstacksize(const pthread_attr_t *restrict attr,
size_t *restrict stacksize);
int
pthread_attr_setguardsize(pthread_attr_t *attr, size_t guardsize);
int
pthread_attr_getguardsize(const pthread_attr_t * restrict attr,
size_t * restrict guardsize);
int
pthread_attr_setstackaddr(pthread_attr_t *attr, void *stackaddr);
int
pthread_attr_getstackaddr(const pthread_attr_t *attr, void **stackaddr);
int
pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate);
int
pthread_attr_getdetachstate(const pthread_attr_t *attr,
int *detachstate);
int
pthread_attr_setinheritsched(pthread_attr_t *attr, int inheritsched);
int
pthread_attr_getinheritsched(const pthread_attr_t *restrict attr,
int *restrct inheritsched);
int
pthread_attr_setschedparam(pthread_attr_t *attr,
const struct sched_param *param);
int
pthread_attr_getschedparam(const pthread_attr_t *attr,
struct sched_param *param);
int
pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy);
int
pthread_attr_getschedpolicy(const pthread_attr_t *restrict attr,
int *restrict policy);
int
pthread_attr_setscope(pthread_attr_t *attr, int contentionscope);
int
pthread_attr_getscope(const pthread_attr_t *restrict attr,
int *restrict contentionscope);
π bestie heads up: copy these signatures exactly as written, character for character. Yes, including restrct. That's literally what the page says and I am not out here editing source material. π
π DESCRIPTION π
So. Thread attributes. They're the little parameter bag you hand to pthread_create() to tell it how you want your shiny new thread to be built. π
The cute part? One attribute object can be used in multiple calls to pthread_create() β with or without modifications between calls. Reuse her, she's flexible. β»οΈ
pthread_attr_init()β initializesattrwith all the default thread attributes. The blank-slate, fresh-face start. β¨pthread_attr_destroy()β destroysattr. She's done, say bye. πpthread_attr_set*()β the setter squad: each one sets the attribute that matches its own name. Set it, slay it. πpthread_attr_get*()β the getter squad: each one copies the value of the attribute that matches its name into the location pointed to by the second function parameter. Reading is fundamental, fr fr. π
π₯ RETURN VALUES π₯
If everything's good: 0. That's the win, that's the vibe check passed. β
Otherwise you get an error number back to tell you what went wrong. Not -1, bestie β an actual error number. Note it. π«‘
π ERRORS π
pthread_attr_init() will fail if:
[ENOMEM]β out of memory. π ran out of room, tragic.
pthread_attr_destroy() will fail if:
[EINVAL]β invalid value forattr.
pthread_attr_setstacksize() and pthread_attr_setstack() will fail if:
[EINVAL]βstacksizeis less thanPTHREAD_STACK_MIN. Too smol. Do not do that. π ββοΈ
pthread_attr_setdetachstate() will fail if:
[EINVAL]β invalid value fordetachstate.
pthread_attr_setinheritsched() will fail if:
[EINVAL]β invalid value forattr.
pthread_attr_setschedparam() will fail if:
[EINVAL]β invalid value forattr.[ENOTSUP]β invalid value forparam.
pthread_attr_setschedpolicy() will fail if:
[EINVAL]β invalid value forattr.[ENOTSUP]β invalid or unsupported value forpolicy.
pthread_attr_setscope() will fail if:
[EINVAL]β invalid value forattr.[ENOTSUP]β invalid or unsupported value forcontentionscope.
π¦ SEE ALSO π¦
Go read these next, they're all part of the lore:
π STANDARDS π
Some of these are POSIX.1 girlies, some are SUSv2. Both valid, both iconic, just different eras. π
pthread_attr_init(),pthread_attr_destroy(),pthread_attr_setstacksize(),pthread_attr_getstacksize(),pthread_attr_setstackaddr(),pthread_attr_getstackaddr(),pthread_attr_setdetachstate(), andpthread_attr_getdetachstate()conform to ISO/IEC 9945-1:1996 (βPOSIX.1β). βpthread_attr_setinheritsched(),pthread_attr_getinheritsched(),pthread_attr_setschedparam(),pthread_attr_getschedparam(),pthread_attr_setschedpolicy(),pthread_attr_getschedpolicy(),pthread_attr_setscope(), andpthread_attr_getscope()conform to Version 2 of the Single UNIX Specification (βSUSv2β). πΈ
Ok that's the whole page, you ate that. π Go forth and set your attributes, bestie β this was the Debian edition, dated August 17, 2018. Mwah. ππ«Άβ¨
π¦ where this page comes from
- freebsd 14.5: base 14.5
- freebsd 15.1: base 15.1
- nixpkgs unstable: source
- Copyright (C) 2000 Jason Evans <jasone@FreeBSD.org>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms