All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
Radiant::Condition Class Reference

Condition for threads. More...

#include <Radiant/Condition.hpp>

Inheritance diagram for Radiant::Condition:
Patterns::NotCopyable

Public Member Functions

 Condition ()
 Constructor.
 
bool wait (Mutex &mutex, unsigned long millisecs=std::numeric_limits< unsigned long >::max())
 Waits on the wait condition for at most the given time. More...
 
bool wait2 (Mutex &mutex, unsigned int &timeoutMs)
 Waits on the wait condition for at most the given time. More...
 
void wakeAll ()
 Wakes all threads waiting on the condition.
 
void wakeAll (Mutex &mutex)
 Wakes all threads waiting on the condition, while locking the given mutx. More...
 
void wakeOne ()
 Wakes one thread waiting on the condition (the woken thread can not be controlled or predicted)
 
void wakeOne (Mutex &mutex)
 Wakes one thread waiting on the condition (the woken thread can not be controlled or predicted) while locking the given mutex. More...
 
 ~Condition ()
 Destructor.
 

Detailed Description

Condition for threads.

Typical use pattern for thread that waits:

mutex.lock();
while(needMoreData())
condition.wait(mutex);
mutex.unlock();

Typical use pattern for thread that informs its children:

mutex.lock();
condition.wakeAll();
mutex.unlock();

Or simply:

condition.wakeAll(mutex);

Member Function Documentation

bool Radiant::Condition::wait ( Mutex mutex,
unsigned long  millisecs = std::numeric_limits< unsigned long >::max() 
)

Waits on the wait condition for at most the given time.

The mutex must be locked by the calling thread. The mutex is released. If the mutex is not locked the function will return immediately.

Parameters
mutexlocked mutex
millisecstimeout in milliseconds
Returns
false if the wait timed out
bool Radiant::Condition::wait2 ( Mutex mutex,
unsigned int &  timeoutMs 
)

Waits on the wait condition for at most the given time.

Decreases the parameter timeoutMs by amount of time waited. If the wait is timed out returns false; otherwise true.

Parameters
mutexlocked mutex
[in,out]timeoutMstimeout in milliseconds to wait
Returns
false if the wait timed out; otherwise true
void Radiant::Condition::wakeAll ( Mutex mutex)

Wakes all threads waiting on the condition, while locking the given mutx.

Parameters
mutexMutex to lock
void Radiant::Condition::wakeOne ( Mutex mutex)

Wakes one thread waiting on the condition (the woken thread can not be controlled or predicted) while locking the given mutex.

Parameters
mutexMutex to lock