A simple encapsulation of pthread condition variables. More...
#include <Robots/LoBot/thread/LoCondition.H>
Classes | |
class | AutoMutex |
Public Member Functions | |
Condition () | |
Default constructor: sets up the pthread condition variable. | |
template<typename F > | |
void | wait (F pred) |
template<typename F > | |
bool | wait (F pred, int timeout) |
template<typename F > | |
void | signal (F pred) |
template<typename F > | |
void | broadcast (F pred) |
template<typename F > | |
void | protect (F func) |
~Condition () |
A simple encapsulation of pthread condition variables.
Definition at line 84 of file LoCondition.H.
lobot::Condition::Condition | ( | ) |
Default constructor: sets up the pthread condition variable.
Definition at line 83 of file LoCondition.C.
lobot::Condition::~Condition | ( | ) |
Clean-up: release the pthread condition variable and its associated mutex.
Definition at line 101 of file LoCondition.C.
void lobot::Condition::broadcast | ( | F | pred | ) | [inline] |
This function signals all threads waiting on this condition variable that the condition has been satisfied.
The function expects to be passed a predicate function or function object that should return true to indicate that the shared data being waited on is in the desired state, i.e., the state in which the condition variable is flagged as being satisfied. If the predicate returns false, the calling thread will not signal the condition variable and waiting threads will continue to wait.
Additionally, the predicate function/object should also perform the necessary operations to actually bring the shared data into the state in which the condition variable can be signaled.
Basically, this function implements the usual pthread signaling pattern for condition variables so that clients need only implement the actual test required to decide whether the condition variable is satisfied or not.
Definition at line 282 of file LoCondition.H.
void lobot::Condition::protect | ( | F | func | ) | [inline] |
This function allows clients to execute some function under the protection of the mutex associated with this condition variable. The condition variable itself is not involved; only its associated mutex comes into play. The mutex is locked prior to calling the client-supplied function or funcion object and unlocked after it.
Definition at line 294 of file LoCondition.H.
void lobot::Condition::signal | ( | F | pred | ) | [inline] |
This function signals any one thread waiting on this condition variable that the condition has been satisfied.
The function expects to be passed a predicate function or function object that should return true to indicate that the shared data being waited on is in the desired state, i.e., the state in which the condition variable is flagged as being satisfied. If the predicate returns false, the calling thread will not signal the condition variable and waiting threads will continue to wait.
Additionally, the predicate function/object should also perform the necessary operations to actually bring the shared data into the state in which the condition variable can be signaled.
Basically, this function implements the usual pthread signaling pattern for condition variables so that clients need only implement the actual test required to decide whether the condition variable is satisfied or not.
Definition at line 273 of file LoCondition.H.
bool lobot::Condition::wait | ( | F | pred, | |
int | timeout | |||
) | [inline] |
This function can be used by clients to wait on this condition variable. The wait is timed, i.e., the calling thread will wait until either the condition is signaled by some other thread or until the wait times out.
This function expects to be passed a predicate function or function object that should return true if the shared data being waited on is in the desired state, i.e., the state in which the condition variable is flagged as being satisfied. If the predicate returns false, the calling thread will continue to wait.
Additionally, the function should be passed a timeout value in milliseconds.
Thus, this function will block the calling thread until the client-supplied test condition becomes true or until the timeout is exceeded.
This function will return true to indicate that the wait completed successfully, i.e., the condition being waited on is signaled. A false return value indicates that the wait timed out.
Basically, this function implements the usual timed-wait pattern to be applied for pthread condition variables so that clients need only implement the actual test required to decide whether the condition variable is satisfied or not.
Definition at line 238 of file LoCondition.H.
void lobot::Condition::wait | ( | F | pred | ) | [inline] |
This function can be used by clients to wait on this condition variable. The wait is forever, i.e., the calling thread will wait until the condition is signaled by some other thread.
This function expects to be passed a predicate function or function object that should return true if the shared data being waited on is in the desired state, i.e., the state in which the condition variable is flagged as being satisfied. If the predicate returns false, the calling thread will continue to wait.
Thus, this function will block the calling thread until the predicate returns true.
Basically, this function implements the usual wait pattern to be applied for pthread condition variables so that clients need only implement the actual test required to decide whether the condition variable is satisfied or not.
Definition at line 229 of file LoCondition.H.