cs24-24fa pthread_cond_init

Introduction to Computing Systems (Fall 2024)

Name

pthread_cond_destroy, pthread_cond_init — destroy and initialize condition variables

Synopsis

1
2
3
4
5
#include <pthread.h>

int pthread_cond_destroy(pthread_cond_t *cond);
int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr);
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

Description

Initialization

The pthread_cond_init() function shall initialize the condition variable referenced by cond with attributes referenced by attr. If attr is NULL, the default condition variable attributes shall be used; the effect is the same as passing the address of a default condition variable attributes object. Upon successful initialization, the state of the condition variable shall become initialized.

Attempting to initialize an already initialized condition variable results in undefined behavior.

Destruction

The pthread_cond_destroy() function shall destroy the given condition variable specified by cond; the object becomes, in effect, uninitialized.

It shall be safe to destroy an initialized condition variable upon which no threads are currently blocked. Attempting to destroy a condition variable upon which other threads are currently blocked results in undefined behavior.

Example

1
2
3
4
5
6
7
8
#include <pthread.h>
pthread_cond_t cond;

int main(int argc, char *argv[]) {
    pthread_cond_init(&cond, NULL);
    pthread_cond_destroy(&cond);
    return 0;
}

Return Value

If successful, the pthread_cond_destroy() and pthread_cond_init() functions shall return zero; otherwise, an error number shall be returned to indicate the error.

See Also

pthread_cond_wait(), pthread_cond_signal()