Generic condition variable. More...
#include <CCondVar.h>
Inherited by CCondVar< T >, and CCondVar< bool >.
Public Member Functions | |
CCondVarBase (CMutex *mutex) | |
manipulators | |
void | lock () const |
Lock the condition variable's mutex. | |
void | unlock () const |
Unlock the condition variable's mutex. | |
void | signal () |
Signal the condition variable. | |
void | broadcast () |
Signal the condition variable. | |
accessors | |
bool | wait (double timeout=-1.0) const |
Wait on the condition variable. | |
bool | wait (CStopwatch &timer, double timeout) const |
Wait on the condition variable. | |
CMutex * | getMutex () const |
Get the mutex. |
Generic condition variable.
This class provides functionality common to all condition variables but doesn't provide the actual variable storage. A condition variable is a multiprocessing primitive that can be waited on. Every condition variable has an associated mutex.
Definition at line 30 of file CCondVar.h.
CCondVarBase::CCondVarBase | ( | CMutex * | mutex | ) |
mutex
must not be NULL. All condition variables have an associated mutex. The mutex needn't be unique to one condition variable.
Definition at line 23 of file CCondVar.cpp.
void CCondVarBase::broadcast | ( | ) |
Signal the condition variable.
Wake up all waiting threads, if any.
Definition at line 54 of file CCondVar.cpp.
CMutex * CCondVarBase::getMutex | ( | ) | const |
void CCondVarBase::lock | ( | ) | const |
Lock the condition variable's mutex.
Lock the condition variable's mutex. The condition variable should be locked before reading or writing it. It must be locked for a call to wait(). Locks are not recursive; locking a locked mutex will deadlock the thread.
Definition at line 36 of file CCondVar.cpp.
References CMutex::lock().
void CCondVarBase::signal | ( | ) |
Signal the condition variable.
Wake up one waiting thread, if there are any. Which thread gets woken is undefined.
Definition at line 48 of file CCondVar.cpp.
bool CCondVarBase::wait | ( | CStopwatch & | timer, | |
double | timeout | |||
) | const |
Wait on the condition variable.
Same as wait(double)
but use timer
to compare against . Since clients normally wait on condition variables in a loop, clients can use this to avoid recalculating timeout
on each iteration. Passing a stopwatch with a negative timeout
is pointless (it will never time out) but permitted.
(cancellation point)
Definition at line 60 of file CCondVar.cpp.
References CStopwatch::getTime(), and wait().
bool CCondVarBase::wait | ( | double | timeout = -1.0 |
) | const |
Wait on the condition variable.
Wait on the condition variable. If timeout
< 0 then wait until signalled, otherwise up to timeout
seconds or until signalled, whichever comes first. Returns true if the object was signalled during the wait, false otherwise.
The proper way to wait for a condition is:
cv.lock();
while (cv-expr) {
cv.wait();
}
cv.unlock();
where cv-expr
involves the value of cv
and is false when the condition is satisfied.
(cancellation point)
Definition at line 72 of file CCondVar.cpp.
Referenced by CTCPSocket::flush(), and wait().