#include <Condition.h>
Public Member Functions | |
void | broadcast () |
Condition () | |
void | signal () |
void | wait (Mutex &mutex) |
void | wait (Mutex &mutex, unsigned int maxTime) |
virtual | ~Condition () |
Private Attributes | |
TCondition * | m_Condition |
bool | m_SignalArrived |
Mutex | m_SignalArrivedMutex |
A synchronization object for one-to-one or one-to-many control events.
The Condition class provides a synchonization object for one-to-one and one-to-many control events. Using this class, it is possible to pause the execution of a thread until it gets a signal from another thread to continue. This signal can be sent to only a single thread (one-to-one event) via signal or to all threads waiting for the condition object (one-to-many) via broadcast.
Note that sometimes signals are issued by the operating system and not only by other threads. So the criterion you are waiting for may not be satisfied. However, this can be handled using a loop:
Condition condition; bool criterion; Mutex mutex; mutex.lock(); ... while( !criterion){ condition.wait(mutex); } ... mutex.unlock();
The signaling thread should use code like the following:
criterion = true; condition.signal(); // or "condition.broadcast()"
Definition at line 53 of file Condition.h.
Default constructor. Creates a new platform-dependent condition object.
Definition at line 22 of file Condition.cpp.
Condition::~Condition | ( | ) | [virtual] |
Destructor. Destroys the instance and releases all associated system resources.
Definition at line 30 of file Condition.cpp.
void Condition::broadcast | ( | ) |
Issue a signal and wake up all threads waiting for this condition. This is useful for a one-to-many control event.
Definition at line 93 of file Condition.cpp.
void Condition::signal | ( | ) |
Issue a signal and wake up a single thread waiting for this condition. This is useful for a one-to-one control event.
Definition at line 84 of file Condition.cpp.
void Condition::wait | ( | Mutex & | mutex | ) |
Wait for a signal. Pauses the thread, unlock's the given mutex and waits for a signal before the execution is continued.
mutex | Mutex that is unlocked until the thread wakes up again |
Definition at line 37 of file Condition.cpp.
void Condition::wait | ( | Mutex & | mutex, |
unsigned int | maxTime | ||
) |
Wait for a signal until a given maximum time has passed
maxTime | Maximal wait time [ms] |
Definition at line 56 of file Condition.cpp.
TCondition* Condition::m_Condition [private] |
Platform-dependent condition representation.
Definition at line 103 of file Condition.h.
bool Condition::m_SignalArrived [private] |
This variable is set to true when a signal arrives. It serves as a reminder in the case that a signal arrives before a process starts waiting on it. After every wait call this variable will be set to false.
Definition at line 109 of file Condition.h.
Mutex Condition::m_SignalArrivedMutex [private] |
This mutex is needed for locking the condition while the variable m_SignalArrived is modified and read by another process. Without this mutex it would be possible that signals arrive before a process starts waiting on them.
Definition at line 116 of file Condition.h.