pthread_attr_init(3)

✨ pthread_attr_init β€” bestie edition ✨

okay so real talk: this is the pthread_attr_init(3) page, from the Library Functions Manual, and it's genuinely the glow-up page for thread attributes. think of it like getting ready before you go out β€” you don't just spawn a thread with whatever vibe, you curate it first. πŸ’…


🌸 NAME 🌸

pthread_attr_init, pthread_attr_destroy β€” the two functions that initialize and destroy your thread attributes object, no cap


πŸ“š LIBRARY πŸ“š

POSIX threads library (libpthread, -lpthread) β€” yes bestie, you gotta link it


πŸ¦‹ SYNOPSIS πŸ¦‹

#include <pthread.h>

int pthread_attr_init(pthread_attr_t *attr);
int pthread_attr_destroy(pthread_attr_t *attr);

πŸ’– DESCRIPTION πŸ’–

So pthread_attr_init() takes that thread attributes object pointed to by attr and initializes it with all the default attribute values. like, a fresh start, a clean slate, quite literally the reset era. 🧚

After you call it, you can set the individual attributes on that object using a whole bunch of related functions (they're all listed down in SEE ALSO, go look, they're right there), and then you can use that object in one or more pthread_create(3) calls to actually create threads. main character energy fr.

🚨 Calling pthread_attr_init() on a thread attributes object that has already been initialized results in undefined behavior. that's the "don't do it, I'm begging" warning, bestie. undefined behavior means literally anything can happen and it's on you 😭

When you don't need that thread attributes object anymore, you should destroy it with pthread_attr_destroy(). important detail: destroying a thread attributes object has no effect on threads that were created using that object. the threads already did their thing, they're independent now, they don't care. 🫢

And once a thread attributes object has been destroyed, you can reinitialize it with pthread_attr_init() β€” glow-up round two, she's back. But any other use of a destroyed thread attributes object has undefined results. so like, don't πŸ‘ touch πŸ‘ it πŸ‘


🌈 RETURN VALUE 🌈

On success, these functions return 0. On error, they return a nonzero error number. simple as that ✨


πŸŽ€ ERRORS πŸŽ€

POSIX.1 documents an ENOMEM error for pthread_attr_init(). but on Linux? these functions always succeed. (still, portable and future-proof applications should nevertheless handle a possible error return β€” it's giving "wear a seatbelt even though you're just going around the block", and honestly, valid.)


πŸ‘‘ ATTRIBUTES πŸ‘‘

For an explanation of the terms used in this section, see attributes(7).

  • Interface: pthread_attr_init(), pthread_attr_destroy()
    • Attribute: Thread safety
    • Value: MT-Safe

⭐ STANDARDS ⭐

POSIX.1-2008. certified. iconic. πŸ’…


πŸ•°οΈ HISTORY πŸ•°οΈ

POSIX.1-2001. she's been around, she's a legend, she's not new here. πŸ‘‘


πŸ“ NOTES πŸ“

The pthread_attr_t type should be treated as opaque. that means no peeking πŸ‘€ β€” any access to the object other than via pthreads functions is nonportable and produces undefined results. hands off the internals, bestie, they're not yours.


🌸 EXAMPLES 🌸

The program below optionally makes use of pthread_attr_init() and various related functions to initialize a thread attributes object that is used to create a single thread. Once created, the thread uses the pthread_getattr_np(3) function (a nonstandard GNU extension, iykyk) to retrieve the thread's attributes, and then displays those attributes. like a vibe check but for pthreads.

If the program is run with no command-line argument, then it passes NULL as the attr argument of pthread_create(3), so that the thread is created with default attributes. Running the program on Linux/x86-32 with the NPTL threading implementation, we see the following:

$ ulimit -s       # No stack limit ==> default stack size is 2 MB
unlimited
$ ./a.out
Thread attributes:
        Detach state        = PTHREAD_CREATE_JOINABLE
        Scope               = PTHREAD_SCOPE_SYSTEM
        Inherit scheduler   = PTHREAD_INHERIT_SCHED
        Scheduling policy   = SCHED_OTHER
        Scheduling priority = 0
        Guard size          = 4096 bytes
        Stack address       = 0x40196000
        Stack size          = 0x201000 bytes

When we supply a stack size as a command-line argument, the program initializes a thread attributes object, sets various attributes in that object, and passes a pointer to the object in the call to pthread_create(3). Running the program on Linux/x86-32 with the NPTL threading implementation, we see the following:

$ ./a.out 0x3000000
posix_memalign() allocated at 0x40197000
Thread attributes:
        Detach state        = PTHREAD_CREATE_DETACHED
        Scope               = PTHREAD_SCOPE_SYSTEM
        Inherit scheduler   = PTHREAD_EXPLICIT_SCHED
        Scheduling policy   = SCHED_OTHER
        Scheduling priority = 0
        Guard size          = 0 bytes
        Stack address       = 0x40197000
        Stack size          = 0x3000000 bytes

🧚 Program source 🧚

run it, read it, it's literally all right here, not me gatekeeping:

#define _GNU_SOURCE     /* To get pthread_getattr_np() declaration */
#include <err.h>
#include <errno.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

static void
display_pthread_attr(pthread_attr_t *attr, char *prefix)
{
    int s, i;
    size_t v;
    void *stkaddr;
    struct sched_param sp;

    s = pthread_attr_getdetachstate(attr, &i);
    if (s != 0)
        errc(EXIT_FAILURE, s, "pthread_attr_getdetachstate");
    printf("%sDetach state        = %s\n", prefix,
           (i == PTHREAD_CREATE_DETACHED) ? "PTHREAD_CREATE_DETACHED" :
           (i == PTHREAD_CREATE_JOINABLE) ? "PTHREAD_CREATE_JOINABLE" :
           "???");

    s = pthread_attr_getscope(attr, &i);
    if (s != 0)
        errc(EXIT_FAILURE, s, "pthread_attr_getscope");
    printf("%sScope               = %s\n", prefix,
           (i == PTHREAD_SCOPE_SYSTEM)  ? "PTHREAD_SCOPE_SYSTEM" :
           (i == PTHREAD_SCOPE_PROCESS) ? "PTHREAD_SCOPE_PROCESS" :
           "???");

    s = pthread_attr_getinheritsched(attr, &i);
    if (s != 0)
        errc(EXIT_FAILURE, s, "pthread_attr_getinheritsched");
    printf("%sInherit scheduler   = %s\n", prefix,
           (i == PTHREAD_INHERIT_SCHED)  ? "PTHREAD_INHERIT_SCHED" :
           (i == PTHREAD_EXPLICIT_SCHED) ? "PTHREAD_EXPLICIT_SCHED" :
           "???");

    s = pthread_attr_getschedpolicy(attr, &i);
    if (s != 0)
        errc(EXIT_FAILURE, s, "pthread_attr_getschedpolicy");
    printf("%sScheduling policy   = %s\n", prefix,
           (i == SCHED_OTHER) ? "SCHED_OTHER" :
           (i == SCHED_FIFO)  ? "SCHED_FIFO" :
           (i == SCHED_RR)    ? "SCHED_RR" :
           "???");

    s = pthread_attr_getschedparam(attr, &sp);
    if (s != 0)
        errc(EXIT_FAILURE, s, "pthread_attr_getschedparam");
    printf("%sScheduling priority = %d\n", prefix, sp.sched_priority);

    s = pthread_attr_getguardsize(attr, &v);
    if (s != 0)
        errc(EXIT_FAILURE, s, "pthread_attr_getguardsize");
    printf("%sGuard size          = %zu bytes\n", prefix, v);

    s = pthread_attr_getstack(attr, &stkaddr, &v);
    if (s != 0)
        errc(EXIT_FAILURE, s, "pthread_attr_getstack");
    printf("%sStack address       = %p\n", prefix, stkaddr);
    printf("%sStack size          = %#zx bytes\n", prefix, v);
}

static void *
thread_start(void *arg)
{
    int s;
    pthread_attr_t gattr;

    /* pthread_getattr_np() is a non-standard GNU extension that
       retrieves the attributes of the thread specified in its
       first argument.  */

    s = pthread_getattr_np(pthread_self(), &gattr);
    if (s != 0)
        errc(EXIT_FAILURE, s, "pthread_getattr_np");

    printf("Thread attributes:\n");
    display_pthread_attr(&gattr, "\t");

    exit(EXIT_SUCCESS);         /* Terminate all threads */
}

int
main(int argc, char *argv[])
{
    pthread_t thr;
    pthread_attr_t attr;
    pthread_attr_t *attrp;      /* NULL or &attr */
    int s;

    attrp = NULL;

    /* If a command-line argument was supplied, use it to set the
       stack-size attribute and set a few other thread attributes,
       and set attrp pointing to thread attributes object.  */

    if (argc > 1) {
        size_t stack_size;
        void *sp;

        attrp = &attr;

        s = pthread_attr_init(&attr);
        if (s != 0)
            errc(EXIT_FAILURE, s, "pthread_attr_init");

        s = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
        if (s != 0)
            errc(EXIT_FAILURE, s, "pthread_attr_setdetachstate");

        s = pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
        if (s != 0)
            errc(EXIT_FAILURE, s, "pthread_attr_setinheritsched");

        stack_size = strtoul(argv[1], NULL, 0);

        s = posix_memalign(&sp, sysconf(_SC_PAGESIZE), stack_size);
        if (s != 0)
            errc(EXIT_FAILURE, s, "posix_memalign");

        printf("posix_memalign() allocated at %p\n", sp);

        s = pthread_attr_setstack(&attr, sp, stack_size);
        if (s != 0)
            errc(EXIT_FAILURE, s, "pthread_attr_setstack");
    }

    s = pthread_create(&thr, attrp, &thread_start, NULL);
    if (s != 0)
        errc(EXIT_FAILURE, s, "pthread_create");

    if (attrp != NULL) {
        s = pthread_attr_destroy(attrp);
        if (s != 0)
            errc(EXIT_FAILURE, s, "pthread_attr_destroy");
    }

    pause();    /* Terminates when other thread calls exit() */
}

πŸ”₯ SEE ALSO πŸ”₯

the whole girl squad, go follow them:


okay that's the whole page, you're fully caught up bestie πŸ’œ this was Linux man-pages 6.20, dated 2025-09-21, and it's the pthread_attr_init(3) page β€” go initialize something responsibly, mwah πŸ’…βœ¨

πŸ“¦ 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
  • πŸ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms