Next: Cancellation, Previous: Basic Thread Operations, Up: POSIX Threads
Threads have a number of attributes that may be set at creation time.
This is done by filling a thread attribute object attr of type
pthread_attr_t
, then passing it as second argument to
pthread_create
. Passing NULL
is equivalent to passing a
thread attribute object with all attributes set to their default values.
Attribute objects are consulted only when creating a new thread. The
same attribute object can be used for creating several threads.
Modifying an attribute object after a call to pthread_create
does
not change the attributes of the thread previously created.
pthread_attr_init
initializes the thread attribute object attr and fills it with default values for the attributes. (The default values are listed below for each attribute.)Each attribute attrname (see below for a list of all attributes) can be individually set using the function
pthread_attr_set
attrname and retrieved using the functionpthread_attr_get
attrname.
pthread_attr_destroy
destroys the attribute object pointed to by attr releasing any resources associated with it. attr is left in an undefined state, and you must not use it again in a call to any pthreads function until it has been reinitialized.
Set attribute attr to value in the attribute object pointed to by obj. See below for a list of possible attributes and the values they can take.
On success, these functions return 0. If value is not meaningful for the attr being modified, they will return the error code
EINVAL
. Some of the functions have other failure modes; see below.
Store the current setting of attr in obj into the variable pointed to by value.
These functions always return 0.
The following thread attributes are supported:
PTHREAD_CREATE_JOINABLE
) or in the detached state
(PTHREAD_CREATE_DETACHED
). The default is
PTHREAD_CREATE_JOINABLE
.
In the joinable state, another thread can synchronize on the thread
termination and recover its termination code using pthread_join
,
but some of the thread resources are kept allocated after the thread
terminates, and reclaimed only when another thread performs
pthread_join
on that thread.
In the detached state, the thread resources are immediately freed when
it terminates, but pthread_join
cannot be used to synchronize on
the thread termination.
A thread created in the joinable state can later be put in the detached
thread using pthread_detach
.
SCHED_OTHER
(regular, non-realtime scheduling), SCHED_RR
(realtime,
round-robin) or SCHED_FIFO
(realtime, first-in first-out).
The default is SCHED_OTHER
.
The realtime scheduling policies SCHED_RR
and SCHED_FIFO
are available only to processes with superuser privileges.
pthread_attr_setschedparam
will fail and return ENOTSUP
if
you try to set a realtime policy when you are unprivileged.
The scheduling policy of a thread can be changed after creation with
pthread_setschedparam
.
This attribute is not significant if the scheduling policy is
SCHED_OTHER
; it only matters for the realtime policies
SCHED_RR
and SCHED_FIFO
.
The scheduling priority of a thread can be changed after creation with
pthread_setschedparam
.
PTHREAD_EXPLICIT_SCHED
) or are inherited from the parent thread
(value PTHREAD_INHERIT_SCHED
). The default is
PTHREAD_EXPLICIT_SCHED
.
PTHREAD_SCOPE_SYSTEM
, meaning that the threads contend
for CPU time with all processes running on the machine. In particular,
thread priorities are interpreted relative to the priorities of all
other processes on the machine. The other possibility,
PTHREAD_SCOPE_PROCESS
, means that scheduling contention occurs
only between the threads of the running process: thread priorities are
interpreted relative to the priorities of the other threads of the
process, regardless of the priorities of other processes.
PTHREAD_SCOPE_PROCESS
is not supported in LinuxThreads. If you
try to set the scope to this value, pthread_attr_setscope
will
fail and return ENOTSUP
.
PTHREAD_STACK_MIN
.
If the value exceeds the system's maximum stack size, or is smaller
than PTHREAD_STACK_MIN
, pthread_attr_setstacksize
will
fail and return EINVAL
.
If the value of stacksize is less than PTHREAD_STACK_MIN
,
or greater than the system's maximum stack size, or if the value of
stackaddr lacks the proper alignment, pthread_attr_setstack
will fail and return EINVAL
.
If the caller is managing their own stacks (if the stackaddr
attribute has been set), then the guardsize
attribute is ignored.
If the value exceeds the stacksize
, pthread_atrr_setguardsize
will fail and return EINVAL
.