ASL
Loading...
Searching...
No Matches
Condition Class Reference

Detailed Description

A condition variable allows one or more threads to wait until a condition is made true by another thread.

The condition or watched resource itself must be protected by a mutex and this mutex given to the condition variable. This is how this is done:

We have the ready flag to wait until it is true:

bool ready = false;
Mutex mutex;
An Array is a contiguous and resizable array of any type of elements.
Definition Array.h:69
A condition variable allows one or more threads to wait until a condition is made true by another thr...
Definition Mutex.h:151
A mutex that can be locked or unlocked to protect concurrent access to a resource.
Definition Mutex.h:27

One thread waits for it to become true like this:

mutex.lock();
while (!ready)
condition.wait();
mutex.unlock();
void unlock()
Unlocks the mutex so other threads can use the protected resource.
Definition Mutex.h:64
void lock()
Locks the mutex.
Definition Mutex.h:49

Another thread, when it is ready signals it like this:

mutex.lock();
ready = true;
condition.signal();
mutex.unlock();

#include <Mutex.h>

Public Member Functions

 Condition (Mutex &m)
 Constructs a condition variable with a mutex to use.
 
void use (Mutex &m)
 Sets mutex to use if it was not given in the constructor.
 
void signal ()
 Wake up waiting threads to signal a condition change.
 
void wait ()
 Wait for the condition change to be signaled.
 
bool wait (double timeout)
 Wait for the condition change to be signaled up to a timeout and return true if there was no signal.
 

The documentation for this class was generated from the following file: