pthread_attr_init (3) - Linux Manuals
pthread_attr_init: initialize and destroy
NAME
pthread_attr_init, pthread_attr_destroy - initialize and destroy thread attributes object
SYNOPSIS
#include <pthread.h> int pthread_attr_init(pthread_attr_t *attr); int pthread_attr_destroy(pthread_attr_t *attr); Compile and link with -pthread.
DESCRIPTION
The pthread_attr_init() function initializes the thread attributes object pointed to by attr with default attribute values. After this call, individual attributes of the object can be set using various related functions (listed under SEE ALSO), and then the object can be used in one or more pthread_create(3) calls that create threads.Calling pthread_attr_init() on a thread attributes object that has already been initialized results in undefined behavior.
When a thread attributes object is no longer required, it should be destroyed using the pthread_attr_destroy() function. Destroying a thread attributes object has no effect on threads that were created using that object.
Once a thread attributes object has been destroyed, it can be reinitialized using pthread_attr_init(). Any other use of a destroyed thread attributes object has undefined results.
RETURN VALUE
On success, these functions return 0; on error, they return a nonzero error number.ERRORS
POSIX.1 documents an ENOMEM error for pthread_attr_init(); on Linux these functions always succeed (but portable and future-proof applications should nevertheless handle a possible error return).ATTRIBUTES
For an explanation of the terms used in this section, see attributes(7).Interface | Attribute | Value |
pthread_attr_init(), pthread_attr_destroy() | Thread safety | MT-Safe |
CONFORMING TO
POSIX.1-2001, POSIX.1-2008.NOTES
The pthread_attr_t type should be treated as opaque: any access to the object other than via pthreads functions is nonportable and produces undefined results.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) to retrieve the thread's attributes, and then displays those attributes.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:
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:
#define handle_error_en(en, msg) \
static void
display_pthread_attr(pthread_attr_t *attr, char *prefix)
{
Program source
#define _GNU_SOURCE /* To get pthread_getattr_np() declaration */
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>