Книга: Programming with POSIX® Threads

9.3.5 Condition variables

9.3.5 Condition variables

Condition variables provide communication, the ability to wait for some shared resource to reach some desired state, or to signal that it has reached some state in which another thread may be interested. Each condition variable is closely associated with a mutex that protects the state of the resource.

pthread_condattr_destroy

int pthread_condattr_destroy (

pthread_condattr_t *attr);

Destroy a condition variable attributes object. The object can no longer be used.

References: 3.3, 5.2.2

Headers: <pthread.h>

Errors: [EINVAL] attr invalid.

Hint: Does not affect condition variables created using attr.

| pthread_condattr_getpshared...............................................[_pos

IX THREAD PROCESS SHARED]

int pthread_condattr_getpshared ( const pthread_condattr_t int

*attr, *pshared);

Determine whether condition variables created with attr can be shared by multiple processes.

pshared

PTHREAD_PROCESS_SHARED May be shared if in shared

memory.

PTHREAD_PROCESS_PRIVATE

Cannot be shared.

References: 3.3, 5.2.2

Headers: <pthread.h>

Errors: [EINVAL]attrinvalid.

Hint: pshared condition variables must be allocated in shared memory

and used with pshared mutexes.

pthread_condattr_init

int pthread_condattr_init (

pthread_condattr_t *attr);

Initialize a condition variable attributes object with default attributes.

References: 3.3, 5.2.2

Headers: <pthread.h>

Errors: [ENOMEM] insufficient memory for attr.

Hint: Use to define condition variable types.

| pthread_condattr_setpshared................................................[_POSIX_THREAD_PRocEss_sHARED]

int pthread_condattr_setpshared (

pthread_condattr_t *attr,

int pshared);

Condition variables created with attr can be shared between processes if the pthread_cond_t variable is allocated in memory shared by the processes.

pshared

PTHREAD_PROCESS_SHARED

PTHREAD_PROCESS_PRIVATE

May be shared if in shared memory.

Cannot be shared.

References: 3.3, 5.2.2 Headers: <pthread.h>

Errors: [EINVAL] attr or detachstate invalid.

Hint: pshared condition variables must be allocated in shared memory

and used with pshared mutexes.

pthread_cond_destroy

int pthread_cond_destroy (

pthread_cond_t *cond);

Destroy condition variable cond that you no longer need.

References: 3.3, 5.2.2

Headers: <pthread.h>

Errors: [EBUSY]condisinuse.

[EINVAL] cond is invalid. Hint: Safest after wakeup from cond, when no other threads will wait.

| pthread_cond_init

int pthread cond init

pthread_cond_t

const pthread_condattr_t

*cond, *attr);

Initialize a condition variable cond. The attr argument specifies optional creation

attributes.

References:

Headers:

Errors:

Hint:

3.3, 5.2.2 <pthread.h>

[EAGAIN] insufficient resources (other than memory).

[ ENOMEM] insufficient memory.

[ EBUSY ] cond is already initialized.

[EINVAL] attr is invalid.

Use static initialization instead, if possible.

pthread_cond_broadcast

int pthread_cond_broadcast (

pthread_cond_t *cond);

Broadcast condition variable cond, waking all current waiters.

References: 3.3, 5.2.2

Headers: <pthread.h>

Errors: [EINVAL]condisinvalid.

Hint: Use when more than one waiter may respond to predicate change

or if any waiting thread may not be able to respond.

pthread_cond_signal

int pthread_cond_signal (

pthread_cond_t *cond);

Signal condition variable cond, waking one waiting thread. If SCHED_FIFO or SCHED_RR policy threads are waiting, the highest-priority waiter is awakened. Otherwise, an unspecified waiter is awakened.

References: 3.3, 5.2.2

Headers: <pthread.h>

Errors: [EINVAL]condisinvalid.

Hint: Use when any waiter can respond, and only one need respond. (All

waiters are equal.)

pthread_cond_timedwait

int pthread cond timedwait

pthread_cond_t pthread_mutex_t const struct timespec

*cond, *mutex, *abstime);

Wait on condition variable cond, until awakened by a signal or broadcast, or until the absolute time abstime is reached.

References:

Headers:

Errors:

Hint:

3.3, 5.2.2 <pthread.h>

[ETIMEDOUT] time specified by abstime has passed.

[EINVAL] cond, mutex, or abstime is invalid.

[EINVAL] different mutexes for concurrent waits.

[EINVAL] mutex is not owned by calling thread.

Mutex is always unlocked (before wait) and relocked (after wait)

inside pthread_cond_timedwait, even if the wait fails, times out, or

is canceled.

pthread_cond_wait

int pthread_cond_wait ( pthread_cond_t pthread_mutex_t

*cond, *mutex);

Wait on condition variable cond, until awakened by a signal or broadcast.

References: 3.3, 5.2.2 Headers: <pthread.h>

Errors: [EINVAL] cond or mutex is invalid.

[EINVAL] different mutexes for concurrent waits. [EINVAL] mutex is not owned by calling thread.

Hint: Mutex is always unlocked (before wait) and relocked (after wait) in-

side pthread_cond_wait, even if the wait fails or is canceled.

Оглавление книги


Генерация: 0.073. Запросов К БД/Cache: 0 / 0
поделиться
Вверх Вниз