COND_INIT(3C) | Standard C Library Functions | COND_INIT(3C) |
cc -mt [ flag... ] file... [ library... ] #include <thread.h> #include <synch.h> int cond_init(cond_t *cvp, int type, void *arg);
int cond_wait(cond_t *cvp, mutex_t *mp);
int cond_timedwait(cond_t *cvp, mutex_t *mp, timestruc_t *abstime);
int cond_reltimedwait(cond_t *cvp, mutex_t *mp, timestruc_t *reltime);
int cond_signal(cond_t *cvp);
int cond_broadcast(cond_t *cvp);
int cond_destroy(cond_t *cvp);
The scope of a condition variable is either intra-process or inter-process. This is dependent upon whether the argument is passed implicitly or explicitly to the initialization of that condition variable. A condition variable does not need to be explicitly initialized. A condition variable is initialized with all zeros, by default, and its scope is set to within the calling process. For inter-process synchronization, a condition variable must be initialized once, and only once, before use.
A condition variable must not be simultaneously initialized by multiple threads or re-initialized while in use by other threads.
Attributes of condition variables can be set to the default or customized at initialization.
The cond_init() function initializes the condition variable pointed to by cvp. A condition variable can have several different types of behavior, specified by type. No current type uses arg although a future type may specify additional behavior parameters with arg. The type argument c take one of the following values:
USYNC_THREAD
USYNC_PROCESS
Initializing condition variables can also be accomplished by allocating in zeroed memory, in which case, a type of USYNC_THREAD is assumed.
If default condition variable attributes are used, statically allocated condition variables can be initialized by the macro DEFAULTCV.
Default condition variable initialization (intra-process):
cond_t cvp; cond_init(&cvp, NULL, NULL); /*initialize condition variable with default*/
or
cond_init(&cvp, USYNC_THREAD, NULL);
or
cond_t cond = DEFAULTCV;
Customized condition variable initialization (inter-process):
cond_init(&cvp, USYNC_PROCESS, NULL); /* initialize cv with inter-process scope */
The cond_wait() function atomically releases the mutex pointed to by mp and causes the calling thread to block on the condition variable pointed to by cvp. The blocked thread may be awakened by cond_signal(), cond_broadcast(), or when interrupted by delivery of a UNIX signal or a fork().
The cond_wait(), cond_timedwait(), and cond_reltimedwait() functions always return with the mutex locked and owned by the calling thread even when returning an error, except when the mutex has the LOCK_ROBUST attribute and has been left irrecoverable by the mutex's last owner. The cond_wait(), cond_timedwait(), and cond_reltimedwait() functions return the appropriate error value if they fail to internally reacquire the mutex.
The cond_signal() function unblocks one thread that is blocked on the condition variable pointed to by cvp.
The cond_broadcast() function unblocks all threads that are blocked on the condition variable pointed to by cvp.
If no threads are blocked on the condition variable, then cond_signal() and cond_broadcast() have no effect.
The cond_signal() or cond_broadcast() functions can be called by a thread whether or not it currently owns the mutex that threads calling cond_wait(), cond_timedwait(), or cond_reltimedwait() have associated with the condition variable during their waits. If, however, predictable scheduling behavior is required, then that mutex should be locked by the thread prior to calling cond_signal() or cond_broadcast().
The cond_destroy() function destroys any state associated with the condition variable pointed to by cvp. The space for storing the condition variable is not freed.
ETIME
The cond_wait(), cond_timedwait(), and cond_reltimedwait() functions will fail if:
EINTR
If the mutex pointed to by mp is a robust mutex (initialized with the LOCK_ROBUST attribute), the cond_wait(), cond_timedwait() and cond_reltimedwait() functions will, under the specified conditions, return the following error values. For complete information, see the description of the mutex_lock() function on the mutex_init(3C) manual page.
ENOTRECOVERABLE
EOWNERDEAD
These functions may fail if:
EFAULT
EINVAL
The cond_wait() function is normally used in a loop testing some condition, as follows:
(void) mutex_lock(mp); while (cond == FALSE) { (void) cond_wait(cvp, mp); } (void) mutex_unlock(mp);
Example 2 Use cond_timedwait() in a loop to test some condition.
The cond_timedwait() function is normally used in a loop testing some condition. It uses an absolute timeout value as follows:
timestruc_t to; ... (void) mutex_lock(mp); to.tv_sec = time(NULL) + TIMEOUT; to.tv_nsec = 0; while (cond == FALSE) { err = cond_timedwait(cvp, mp, &to); if (err == ETIME) { /* timeout, do something */ break; } } (void) mutex_unlock(mp);
Example 3 Use cond_reltimedwait() in a loop to test some condition.
The cond_reltimedwait() function is normally used in a loop testing in some condition. It uses a relative timeout value as follows:
timestruc_t to; ... (void) mutex_lock(mp); while (cond == FALSE) { to.tv_sec = TIMEOUT; to.tv_nsec = 0; err = cond_reltimedwait(cvp, mp, &to); if (err == ETIME) { /* timeout, do something */ break; } } (void) mutex_unlock(mp);
ATTRIBUTE TYPE | ATTRIBUTE VALUE |
MT-Level | MT-Safe |
When cond_wait() returns the value of the condition is indeterminate and must be reevaluated.
The cond_timedwait() and cond_reltimedwait() functions are similar to cond_wait(), except that the calling thread will not wait for the condition to become true past the absolute time specified by abstime or the relative time specified by reltime. Note that cond_timedwait() or cond_reltimedwait() might continue to block as it trys to reacquire the mutex pointed to by mp, which may be locked by another thread. If either cond_timedwait() or cond_reltimedwait() returns because of a timeout, it returns the error value ETIME.
February 15, 2020 |