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_setattrname
and retrieved using the function
pthread_attr_getattrname
.
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.
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.
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
.
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
.
Go to the first, previous, next, last section, table of contents.