pthread_cond_init(3thr)pthread_cond_init(3thr)Namepthread_cond_init - Creates a condition variable.
Syntax
#include <pthread.h>
int pthread_cond_init (ond, attr)
pthread_cond_t *cond;
pthread_condattr_t attr;
Arguments
cond Condition variable that is created.
attr Condition variable attributes object that defines the charac‐
teristics of the condition variable created. If you specify
pthread_condattr_default, default attributes are used.
Description
This routine creates and initializes a condition variable. A condition
variable is a synchronization object used in conjunction with a mutex.
A mutex controls access to shared data; a condition variable allows
threads to wait for that data to enter a defined state. The state is
defined by a Boolean expression called a predicate.
A condition variable is signaled or broadcast to indicate that a predi‐
cate might have become true. The broadcast operation indicates that all
waiting threads should resume and reevaluate the predicate. The signal
operation is used when any one waiting thread can continue.
If a thread that holds a mutex determines that the shared data is not
in the correct state for it to proceed (the associated predicate is not
true), it waits on a condition variable associated with the desired
state. Waiting on the condition variable automatically releases the
mutex so that other threads can modify or examine the shared data. When
a thread modifies the state of the shared data so that a predicate
might be true, it signals or broadcasts on the appropriate condition
variable so that threads waiting for that predicate can continue.
It is important that all threads waiting on a particular condition
variable at any time hold the same mutex. If they do not, the behavior
of the wait operation is unpredictable (an implementation can use the
mutex to control internal access to the condition variable object).
However, it is legal for a client to store condition variables and
mutexes and later reuse them in different combinations. The client must
ensure that no threads use the condition variable with the old mutex.
At any time, an arbitrary number of condition variables can be associ‐
ated with a single mutex, each representing a different predicate of
the shared data protected by that mutex.
Condition variables are not owned by a particular thread. Any associ‐
ated storage is not automatically deallocated when the creating thread
terminates.
Return Values
If an error condition occurs, this routine returns -1, the condition
variable is not initialized, and the contents of cond are undefined.
This routine sets errno to the corresponding error value. Possible
return values are as follows.
Return Error Description
0 Successful comple‐
tion.
-1 [EAGAIN] The system lacks
the necessary
resources to ini‐
tialize another
condition variable.
The system-imposed
limit on the total
number of condition
variables under
execution by a sin‐
gle user is
exceeded.
-1 [ENOMEM] Insufficient memory
exists to initial‐
ize the condition
variable.
pthread_cond_init(3thr)