Standards / Extensions | C or C++ | Dependencies |
---|---|---|
POSIX.4a |
both | POSIX(ON) |
#define _OPEN_THREADS
#include <pthread.h>
int pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *attr);
#define _UNIX03_THREADS
#include <pthread.h>
int pthread_cond_init(pthread_cond_t * __restrict__cond,
pthread_condattr_t * __restrict__attr);
Initializes the condition variable referenced by cond with attributes referenced by attr. If attr is NULL, the default condition variable attributes are used.
If successful, pthread_cond_init() returns 0.
Special behavior for Single UNIX Specification, Version 3: If unsuccessful, pthread_cond_init() returns an error number to indicate the error.
The _OPEN_SYS_MUTEX_EXT feature switch can be optionally included. If the feature is set, then significantly larger pthread_cond_t objects will be defined. The feature is used for the management of mutex and condition variables in shared memory. If the feature switch is set in the define of the condition variables in shared memory, then the same feature switch must be set in the define of the mutex associated with the condition variables.
If the supplied extended pthread_cond_t object is not in shared memory, pthread_cond_init() will treat the object as a non-shared object, since it is not accessible to any other process.
If the _OPEN_SYS_MUTEX_EXT feature switch is set, a shared condition variable is tied to the specified mutex for the life of the condition variable and mutex the very first time a pthread_cond_wait() or pthread_cond_timedwait() is issued. No other mutex can be associated with the specified condition variable or vise versa until the condition variable or mutex is destroyed.
It is recommended that you define and initialize pthread_cond_t objects in the same compile unit. If you pass a pthread_cond_t object around to be initialized, make sure the initialization code has been compiled with the same _OPEN_SYS_MUTEX_EXT feature setting as the code that defines the object.
⁄* CELEBP19 *⁄
#define _OPEN_THREADS
#include <pthread.h>
#include <stdio.h>
main() {
pthread_cond_t cond;
if (pthread_cond_init(&cond, NULL) != 0) {
perror("pthread_cond_init() error");
exit(1);
}
if (pthread_cond_destroy(&cond) != 0) {
perror("pthread_cond_destroy() error");
exit(2);
}
}