PTHREAD_ATTR(3)

πŸ“¦ version 15.1, in freebsd 14.5, freebsd 15.1, nixpkgs unstable

πŸ’… 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() β€” initializes attr with all the default thread attributes. The blank-slate, fresh-face start. ✨
  • pthread_attr_destroy() β€” destroys attr. 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 for attr.

pthread_attr_setstacksize() and pthread_attr_setstack() will fail if:

  • [EINVAL] β€” stacksize is less than PTHREAD_STACK_MIN. Too smol. Do not do that. πŸ™…β€β™€οΈ

pthread_attr_setdetachstate() will fail if:

  • [EINVAL] β€” invalid value for detachstate.

pthread_attr_setinheritsched() will fail if:

  • [EINVAL] β€” invalid value for attr.

pthread_attr_setschedparam() will fail if:

  • [EINVAL] β€” invalid value for attr.
  • [ENOTSUP] β€” invalid value for param.

pthread_attr_setschedpolicy() will fail if:

  • [EINVAL] β€” invalid value for attr.
  • [ENOTSUP] β€” invalid or unsupported value for policy.

pthread_attr_setscope() will fail if:

  • [EINVAL] β€” invalid value for attr.
  • [ENOTSUP] β€” invalid or unsupported value for contentionscope.

πŸ¦‹ 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(), and pthread_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(), and pthread_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