Blame man3/pthread_attr_init.3

Packit 7cfc04
.\" Copyright (c) 2008 Linux Foundation, written by Michael Kerrisk
Packit 7cfc04
.\"     <mtk.manpages@gmail.com>
Packit 7cfc04
.\"
Packit 7cfc04
.\" %%%LICENSE_START(VERBATIM)
Packit 7cfc04
.\" Permission is granted to make and distribute verbatim copies of this
Packit 7cfc04
.\" manual provided the copyright notice and this permission notice are
Packit 7cfc04
.\" preserved on all copies.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Permission is granted to copy and distribute modified versions of this
Packit 7cfc04
.\" manual under the conditions for verbatim copying, provided that the
Packit 7cfc04
.\" entire resulting derived work is distributed under the terms of a
Packit 7cfc04
.\" permission notice identical to this one.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Since the Linux kernel and libraries are constantly changing, this
Packit 7cfc04
.\" manual page may be incorrect or out-of-date.  The author(s) assume no
Packit 7cfc04
.\" responsibility for errors or omissions, or for damages resulting from
Packit 7cfc04
.\" the use of the information contained herein.  The author(s) may not
Packit 7cfc04
.\" have taken the same level of care in the production of this manual,
Packit 7cfc04
.\" which is licensed free of charge, as they might when working
Packit 7cfc04
.\" professionally.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Formatted or processed versions of this manual, if unaccompanied by
Packit 7cfc04
.\" the source, must acknowledge the copyright and authors of this work.
Packit 7cfc04
.\" %%%LICENSE_END
Packit 7cfc04
.\"
Packit 7cfc04
.TH PTHREAD_ATTR_INIT 3 2017-09-15 "Linux" "Linux Programmer's Manual"
Packit 7cfc04
.SH NAME
Packit 7cfc04
pthread_attr_init, pthread_attr_destroy \- initialize and destroy
Packit 7cfc04
thread attributes object
Packit 7cfc04
.SH SYNOPSIS
Packit 7cfc04
.nf
Packit 7cfc04
.B #include <pthread.h>
Packit 7cfc04
.PP
Packit 7cfc04
.BI "int pthread_attr_init(pthread_attr_t *" attr );
Packit 7cfc04
.BI "int pthread_attr_destroy(pthread_attr_t *" attr );
Packit 7cfc04
.PP
Packit 7cfc04
Compile and link with \fI\-pthread\fP.
Packit 7cfc04
.fi
Packit 7cfc04
.SH DESCRIPTION
Packit 7cfc04
The
Packit 7cfc04
.BR pthread_attr_init ()
Packit 7cfc04
function initializes the thread attributes object pointed to by
Packit 7cfc04
.IR attr
Packit 7cfc04
with default attribute values.
Packit 7cfc04
After this call, individual attributes of the object can be set
Packit 7cfc04
using various related functions (listed under SEE ALSO),
Packit 7cfc04
and then the object can be used in one or more
Packit 7cfc04
.BR pthread_create (3)
Packit 7cfc04
calls that create threads.
Packit 7cfc04
.PP
Packit 7cfc04
Calling
Packit 7cfc04
.BR pthread_attr_init ()
Packit 7cfc04
on a thread attributes object that has already been initialized
Packit 7cfc04
results in undefined behavior.
Packit 7cfc04
.PP
Packit 7cfc04
When a thread attributes object is no longer required,
Packit 7cfc04
it should be destroyed using the
Packit 7cfc04
.BR pthread_attr_destroy ()
Packit 7cfc04
function.
Packit 7cfc04
Destroying a thread attributes object has no effect
Packit 7cfc04
on threads that were created using that object.
Packit 7cfc04
.PP
Packit 7cfc04
Once a thread attributes object has been destroyed,
Packit 7cfc04
it can be reinitialized using
Packit 7cfc04
.BR pthread_attr_init ().
Packit 7cfc04
Any other use of a destroyed thread attributes object
Packit 7cfc04
has undefined results.
Packit 7cfc04
.SH RETURN VALUE
Packit 7cfc04
On success, these functions return 0;
Packit 7cfc04
on error, they return a nonzero error number.
Packit 7cfc04
.SH ERRORS
Packit 7cfc04
POSIX.1 documents an
Packit 7cfc04
.B ENOMEM
Packit 7cfc04
error for
Packit 7cfc04
.BR pthread_attr_init ();
Packit 7cfc04
on Linux these functions always succeed
Packit 7cfc04
(but portable and future-proof applications should nevertheless
Packit 7cfc04
handle a possible error return).
Packit 7cfc04
.SH ATTRIBUTES
Packit 7cfc04
For an explanation of the terms used in this section, see
Packit 7cfc04
.BR attributes (7).
Packit 7cfc04
.ad l
Packit 7cfc04
.TS
Packit 7cfc04
allbox;
Packit 7cfc04
lbw22 lb lb
Packit 7cfc04
l l l.
Packit 7cfc04
Interface	Attribute	Value
Packit 7cfc04
T{
Packit 7cfc04
.BR pthread_attr_init (),
Packit 7cfc04
.BR pthread_attr_destroy ()
Packit 7cfc04
T}	Thread safety	MT-Safe
Packit 7cfc04
.TE
Packit 7cfc04
.ad
Packit 7cfc04
.SH CONFORMING TO
Packit 7cfc04
POSIX.1-2001, POSIX.1-2008.
Packit 7cfc04
.SH NOTES
Packit 7cfc04
The
Packit 7cfc04
.I pthread_attr_t
Packit 7cfc04
type should be treated as opaque:
Packit 7cfc04
any access to the object other than via pthreads functions
Packit 7cfc04
is nonportable and produces undefined results.
Packit 7cfc04
.SH EXAMPLE
Packit 7cfc04
The program below optionally makes use of
Packit 7cfc04
.BR pthread_attr_init ()
Packit 7cfc04
and various related functions to initialize a thread attributes
Packit 7cfc04
object that is used to create a single thread.
Packit 7cfc04
Once created, the thread uses the
Packit 7cfc04
.BR pthread_getattr_np (3)
Packit 7cfc04
function (a nonstandard GNU extension) to retrieve the thread's
Packit 7cfc04
attributes, and then displays those attributes.
Packit 7cfc04
.PP
Packit 7cfc04
If the program is run with no command-line argument,
Packit 7cfc04
then it passes NULL as the
Packit 7cfc04
.I attr
Packit 7cfc04
argument of
Packit 7cfc04
.BR pthread_create (3),
Packit 7cfc04
so that the thread is created with default attributes.
Packit 7cfc04
Running the program on Linux/x86-32 with the NPTL threading implementation,
Packit 7cfc04
we see the following:
Packit 7cfc04
.PP
Packit 7cfc04
.in +4n
Packit 7cfc04
.EX
Packit 7cfc04
.\" Results from glibc 2.8, SUSE 11.0; Oct 2008
Packit 7cfc04
.RB "$" " ulimit \-s" "       # No stack limit ==> default stack size is 2 MB"
Packit 7cfc04
unlimited
Packit 7cfc04
.RB "$" " ./a.out"
Packit 7cfc04
Thread attributes:
Packit 7cfc04
        Detach state        = PTHREAD_CREATE_JOINABLE
Packit 7cfc04
        Scope               = PTHREAD_SCOPE_SYSTEM
Packit 7cfc04
        Inherit scheduler   = PTHREAD_INHERIT_SCHED
Packit 7cfc04
        Scheduling policy   = SCHED_OTHER
Packit 7cfc04
        Scheduling priority = 0
Packit 7cfc04
        Guard size          = 4096 bytes
Packit 7cfc04
        Stack address       = 0x40196000
Packit 7cfc04
        Stack size          = 0x201000 bytes
Packit 7cfc04
.EE
Packit 7cfc04
.in
Packit 7cfc04
.PP
Packit 7cfc04
When we supply a stack size as a command-line argument,
Packit 7cfc04
the program initializes a thread attributes object,
Packit 7cfc04
sets various attributes in that object,
Packit 7cfc04
and passes a pointer to the object in the call to
Packit 7cfc04
.BR pthread_create (3).
Packit 7cfc04
Running the program on Linux/x86-32 with the NPTL threading implementation,
Packit 7cfc04
we see the following:
Packit 7cfc04
.PP
Packit 7cfc04
.in +4n
Packit 7cfc04
.EX
Packit 7cfc04
.\" Results from glibc 2.8, SUSE 11.0; Oct 2008
Packit 7cfc04
.RB "$" " ./a.out 0x3000000"
Packit 7cfc04
posix_memalign() allocated at 0x40197000
Packit 7cfc04
Thread attributes:
Packit 7cfc04
        Detach state        = PTHREAD_CREATE_DETACHED
Packit 7cfc04
        Scope               = PTHREAD_SCOPE_SYSTEM
Packit 7cfc04
        Inherit scheduler   = PTHREAD_EXPLICIT_SCHED
Packit 7cfc04
        Scheduling policy   = SCHED_OTHER
Packit 7cfc04
        Scheduling priority = 0
Packit 7cfc04
        Guard size          = 0 bytes
Packit 7cfc04
        Stack address       = 0x40197000
Packit 7cfc04
        Stack size          = 0x3000000 bytes
Packit 7cfc04
.EE
Packit 7cfc04
.in
Packit 7cfc04
.SS Program source
Packit 7cfc04
\&
Packit 7cfc04
.EX
Packit 7cfc04
#define _GNU_SOURCE     /* To get pthread_getattr_np() declaration */
Packit 7cfc04
#include <pthread.h>
Packit 7cfc04
#include <stdio.h>
Packit 7cfc04
#include <stdlib.h>
Packit 7cfc04
#include <unistd.h>
Packit 7cfc04
#include <errno.h>
Packit 7cfc04
Packit 7cfc04
#define handle_error_en(en, msg) \\
Packit 7cfc04
        do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
Packit 7cfc04
Packit 7cfc04
static void
Packit 7cfc04
display_pthread_attr(pthread_attr_t *attr, char *prefix)
Packit 7cfc04
{
Packit 7cfc04
    int s, i;
Packit 7cfc04
    size_t v;
Packit 7cfc04
    void *stkaddr;
Packit 7cfc04
    struct sched_param sp;
Packit 7cfc04
Packit 7cfc04
    s = pthread_attr_getdetachstate(attr, &i);
Packit 7cfc04
    if (s != 0)
Packit 7cfc04
        handle_error_en(s, "pthread_attr_getdetachstate");
Packit 7cfc04
    printf("%sDetach state        = %s\\n", prefix,
Packit 7cfc04
            (i == PTHREAD_CREATE_DETACHED) ? "PTHREAD_CREATE_DETACHED" :
Packit 7cfc04
            (i == PTHREAD_CREATE_JOINABLE) ? "PTHREAD_CREATE_JOINABLE" :
Packit 7cfc04
            "???");
Packit 7cfc04
Packit 7cfc04
    s = pthread_attr_getscope(attr, &i);
Packit 7cfc04
    if (s != 0)
Packit 7cfc04
        handle_error_en(s, "pthread_attr_getscope");
Packit 7cfc04
    printf("%sScope               = %s\\n", prefix,
Packit 7cfc04
            (i == PTHREAD_SCOPE_SYSTEM)  ? "PTHREAD_SCOPE_SYSTEM" :
Packit 7cfc04
            (i == PTHREAD_SCOPE_PROCESS) ? "PTHREAD_SCOPE_PROCESS" :
Packit 7cfc04
            "???");
Packit 7cfc04
Packit 7cfc04
    s = pthread_attr_getinheritsched(attr, &i);
Packit 7cfc04
    if (s != 0)
Packit 7cfc04
        handle_error_en(s, "pthread_attr_getinheritsched");
Packit 7cfc04
    printf("%sInherit scheduler   = %s\\n", prefix,
Packit 7cfc04
            (i == PTHREAD_INHERIT_SCHED)  ? "PTHREAD_INHERIT_SCHED" :
Packit 7cfc04
            (i == PTHREAD_EXPLICIT_SCHED) ? "PTHREAD_EXPLICIT_SCHED" :
Packit 7cfc04
            "???");
Packit 7cfc04
Packit 7cfc04
    s = pthread_attr_getschedpolicy(attr, &i);
Packit 7cfc04
    if (s != 0)
Packit 7cfc04
        handle_error_en(s, "pthread_attr_getschedpolicy");
Packit 7cfc04
    printf("%sScheduling policy   = %s\\n", prefix,
Packit 7cfc04
            (i == SCHED_OTHER) ? "SCHED_OTHER" :
Packit 7cfc04
            (i == SCHED_FIFO)  ? "SCHED_FIFO" :
Packit 7cfc04
            (i == SCHED_RR)    ? "SCHED_RR" :
Packit 7cfc04
            "???");
Packit 7cfc04
Packit 7cfc04
    s = pthread_attr_getschedparam(attr, &sp);
Packit 7cfc04
    if (s != 0)
Packit 7cfc04
        handle_error_en(s, "pthread_attr_getschedparam");
Packit 7cfc04
    printf("%sScheduling priority = %d\\n", prefix, sp.sched_priority);
Packit 7cfc04
Packit 7cfc04
    s = pthread_attr_getguardsize(attr, &v);
Packit 7cfc04
    if (s != 0)
Packit 7cfc04
        handle_error_en(s, "pthread_attr_getguardsize");
Packit 7cfc04
    printf("%sGuard size          = %d bytes\\n", prefix, v);
Packit 7cfc04
Packit 7cfc04
    s = pthread_attr_getstack(attr, &stkaddr, &v);
Packit 7cfc04
    if (s != 0)
Packit 7cfc04
        handle_error_en(s, "pthread_attr_getstack");
Packit 7cfc04
    printf("%sStack address       = %p\\n", prefix, stkaddr);
Packit 7cfc04
    printf("%sStack size          = 0x%zx bytes\\n", prefix, v);
Packit 7cfc04
}
Packit 7cfc04
Packit 7cfc04
static void *
Packit 7cfc04
thread_start(void *arg)
Packit 7cfc04
{
Packit 7cfc04
    int s;
Packit 7cfc04
    pthread_attr_t gattr;
Packit 7cfc04
Packit 7cfc04
    /* pthread_getattr_np() is a non\-standard GNU extension that
Packit 7cfc04
       retrieves the attributes of the thread specified in its
Packit 7cfc04
       first argument */
Packit 7cfc04
Packit 7cfc04
    s = pthread_getattr_np(pthread_self(), &gattr);
Packit 7cfc04
    if (s != 0)
Packit 7cfc04
        handle_error_en(s, "pthread_getattr_np");
Packit 7cfc04
Packit 7cfc04
    printf("Thread attributes:\\n");
Packit 7cfc04
    display_pthread_attr(&gattr, "\\t");
Packit 7cfc04
Packit 7cfc04
    exit(EXIT_SUCCESS);         /* Terminate all threads */
Packit 7cfc04
}
Packit 7cfc04
Packit 7cfc04
int
Packit 7cfc04
main(int argc, char *argv[])
Packit 7cfc04
{
Packit 7cfc04
    pthread_t thr;
Packit 7cfc04
    pthread_attr_t attr;
Packit 7cfc04
    pthread_attr_t *attrp;      /* NULL or &attr */
Packit 7cfc04
    int s;
Packit 7cfc04
Packit 7cfc04
    attrp = NULL;
Packit 7cfc04
Packit 7cfc04
    /* If a command\-line argument was supplied, use it to set the
Packit 7cfc04
       stack\-size attribute and set a few other thread attributes,
Packit 7cfc04
       and set attrp pointing to thread attributes object */
Packit 7cfc04
Packit 7cfc04
    if (argc > 1) {
Packit 7cfc04
        int stack_size;
Packit 7cfc04
        void *sp;
Packit 7cfc04
Packit 7cfc04
        attrp = &att;;
Packit 7cfc04
Packit 7cfc04
        s = pthread_attr_init(&attr);
Packit 7cfc04
        if (s != 0)
Packit 7cfc04
            handle_error_en(s, "pthread_attr_init");
Packit 7cfc04
Packit 7cfc04
        s = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
Packit 7cfc04
        if (s != 0)
Packit 7cfc04
            handle_error_en(s, "pthread_attr_setdetachstate");
Packit 7cfc04
Packit 7cfc04
        s = pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
Packit 7cfc04
        if (s != 0)
Packit 7cfc04
            handle_error_en(s, "pthread_attr_setinheritsched");
Packit 7cfc04
Packit 7cfc04
        stack_size = strtoul(argv[1], NULL, 0);
Packit 7cfc04
Packit 7cfc04
        s = posix_memalign(&sp, sysconf(_SC_PAGESIZE), stack_size);
Packit 7cfc04
        if (s != 0)
Packit 7cfc04
            handle_error_en(s, "posix_memalign");
Packit 7cfc04
Packit 7cfc04
        printf("posix_memalign() allocated at %p\\n", sp);
Packit 7cfc04
Packit 7cfc04
        s = pthread_attr_setstack(&attr, sp, stack_size);
Packit 7cfc04
        if (s != 0)
Packit 7cfc04
            handle_error_en(s, "pthread_attr_setstack");
Packit 7cfc04
    }
Packit 7cfc04
Packit 7cfc04
    s = pthread_create(&thr, attrp, &thread_start, NULL);
Packit 7cfc04
    if (s != 0)
Packit 7cfc04
        handle_error_en(s, "pthread_create");
Packit 7cfc04
Packit 7cfc04
    if (attrp != NULL) {
Packit 7cfc04
        s = pthread_attr_destroy(attrp);
Packit 7cfc04
        if (s != 0)
Packit 7cfc04
            handle_error_en(s, "pthread_attr_destroy");
Packit 7cfc04
    }
Packit 7cfc04
Packit 7cfc04
    pause();    /* Terminates when other thread calls exit() */
Packit 7cfc04
}
Packit 7cfc04
.EE
Packit 7cfc04
.SH SEE ALSO
Packit 7cfc04
.ad l
Packit 7cfc04
.nh
Packit 7cfc04
.BR pthread_attr_setaffinity_np (3),
Packit 7cfc04
.BR pthread_attr_setdetachstate (3),
Packit 7cfc04
.BR pthread_attr_setguardsize (3),
Packit 7cfc04
.BR pthread_attr_setinheritsched (3),
Packit 7cfc04
.BR pthread_attr_setschedparam (3),
Packit 7cfc04
.BR pthread_attr_setschedpolicy (3),
Packit 7cfc04
.BR pthread_attr_setscope (3),
Packit 7cfc04
.BR pthread_attr_setstack (3),
Packit 7cfc04
.BR pthread_attr_setstackaddr (3),
Packit 7cfc04
.BR pthread_attr_setstacksize (3),
Packit 7cfc04
.BR pthread_create (3),
Packit 7cfc04
.BR pthread_getattr_np (3),
Packit 7cfc04
.BR pthread_setattr_default_np (3),
Packit 7cfc04
.BR pthreads (7)
Packit 7cfc04
.SH COLOPHON
Packit 7cfc04
This page is part of release 4.15 of the Linux
Packit 7cfc04
.I man-pages
Packit 7cfc04
project.
Packit 7cfc04
A description of the project,
Packit 7cfc04
information about reporting bugs,
Packit 7cfc04
and the latest version of this page,
Packit 7cfc04
can be found at
Packit 7cfc04
\%https://www.kernel.org/doc/man\-pages/.