#include <ConditionVariable.h>
Collaboration diagram for GNE::ConditionVariable:
Public Member Functions | |
ConditionVariable () | |
Initalizes this class creating a new mutex. | |
ConditionVariable (Mutex *m) | |
Initalizes this class, with another mutex that the caller is responsible for allocating and destroying. | |
void | acquire () |
Locks the associated mutex for this condition variable. | |
void | release () |
Releases the associated mutex. | |
void | wait () |
This method will block until the thread is woken up by a call to signal or broadcast. | |
void | timedWait (int ms) |
This method works identical to wait, except the thread will wake up anyways if not woken up before the timeout. | |
void | timedWait (const Time &until) |
Waits until the given time, specified in absolute time. | |
void | signal () |
Wakes up at least one thread waiting on this CV. | |
void | broadcast () |
Wakes up all threads waiting on this CV. |
This is to be used when a thread needs to wait for a condition that another thread will trigger. The most common usage of ConditionVariables:
cv.lock(); while (!condition) { cv.wait(); } //do stuff cv.unlock();
When cv.wait is called, the thread atomically releases the associated mutex and waits for another thread to call signal or broadcast on the cv object. When this thread reawakens, it waits to lock the associated mutex then exits the wait call.
GNE::ConditionVariable::ConditionVariable | ( | Mutex * | m | ) |
Initalizes this class, with another mutex that the caller is responsible for allocating and destroying.
m | the mutex to be associated with this conditional variable. this mutex will not be deallocated. |
void GNE::ConditionVariable::acquire | ( | ) |
void GNE::ConditionVariable::release | ( | ) |
void GNE::ConditionVariable::signal | ( | ) |
Wakes up at least one thread waiting on this CV.
Which thread wakes up is implementation dependant.
void GNE::ConditionVariable::timedWait | ( | int | ms | ) |
This method works identical to wait, except the thread will wake up anyways if not woken up before the timeout.
The mutex will still be reacquired.
timeout | the time to wait in milliseconds |
void GNE::ConditionVariable::wait | ( | ) |