This struct wraps C++ standard library types std::mutex
and std::condition_variable
. It allows one or more threads to sleep until any other thread satisfies a condition and wakes one or all threads up.
this
-pointer to that type and access its interface. The derived type has to provide only one function member: bool isConditionMet()
true
if the condition is fulfilled, otherwise false
.true
if the instance was notified.TDerived | The derived type which provides method isConditionMet. |
Definition at line 190 of file condition.hpp.
#include <condition.hpp>
Public Field Index: | |
std::condition_variable | conditionVariable |
The condition variable used for blocking and notification. | |
DbgConditionAsserter | Dbg |
The debug tool instance. | |
std::mutex | mutex |
The mutex used for locking this instance. | |
Public Method Index: | |
TCondition (const String &dbgName) | |
void | Acquire (ALIB_DBG_TAKE_CI) |
TDerived & | cast () |
void | Release (ALIB_DBG_TAKE_CI) |
void | ReleaseAndNotify (ALIB_DBG_TAKE_CI) |
void | ReleaseAndNotifyAll (ALIB_DBG_TAKE_CI) |
void | WaitForNotification (ALIB_DBG_TAKE_CI) |
void | WaitForNotification (const Ticks &wakeUpTime, const CallerInfo &ci) |
void | WaitForNotification (const Ticks::Duration &maxWaitTimeSpan, const CallerInfo &ci) |
void | WaitForNotification (const Ticks::Duration::TDuration &maxWaitTimeSpan, const CallerInfo &ci) |
std::condition_variable conditionVariable |
The condition variable used for blocking and notification.
Definition at line 196 of file condition.hpp.
DbgConditionAsserter Dbg |
The debug tool instance.
Definition at line 205 of file condition.hpp.
std::mutex mutex |
The mutex used for locking this instance.
Definition at line 193 of file condition.hpp.
|
inline |
Constructor.
dbgName | The name of this instance. Available only with debug-builds. With release-builds, this type is default constructed. Hence, this parameter has to be provided using macro ALIB_DBG, which prunes the name away. |
Definition at line 213 of file condition.hpp.
|
inline |
A thread which invokes this method gets registered as the current owner of this object, until the same thread releases the ownership invoking Release. In the case that this object is already owned by another thread, the invoking thread is suspended until ownership can be gained.
Multiple (nested) calls to this method are not supported and constitute undefined behavior. In debug-compilations, an assertion is raised.
An instance has to be acquired before invoking any of the notifiy- or wait-methods. When return from a notification method, the instance is released. With return from a wait method, the instance is still held.
Definition at line 233 of file condition.hpp.
|
inline |
Helper to statically casts this object to the derived type.
*this
cast to a reference of TDerived. Definition at line 200 of file condition.hpp.
|
inline |
Releases ownership of this object. If this method is invoked on an object that is not acquired or acquired by a different thread, in debug-compilations an assertion is raised. In release compilations, this leads to undefined behavior.
Definition at line 259 of file condition.hpp.
|
inline |
Unblock a next waiting thread.
As the method name indicates, with this implementation, it is necessary to acquire this object before invoking this method. Internally, the mutex will be released, and thus no separate call to Release is necessary, respectively allowed.
Definition at line 282 of file condition.hpp.
|
inline |
Releases the internal mutex and wakes up all sleeping threads.
Definition at line 305 of file condition.hpp.
|
inline |
Waits for notification (for an unlimited time).
Before invoking this method, this object has to be acquired. After the wake-up call, the internal mutex is (again) acquired and thus has to be released by the owner.
It is allowed to create (endless) loops that never actively release this instance but call one of the waiting methods instead.
Note that "spurious wake-ups" are internally caught with this implementation.
Definition at line 336 of file condition.hpp.
|
inline |
Same as WaitForNotification(ALIB_DBG_TAKE_CI), but takes a point in time at which waiting stops.
wakeUpTime | The point in time to wake up, even if not notified. |
ci | Caller information. Use macro ALIB_COMMA_CALLER_PRUNED with invocations. |
Definition at line 432 of file condition.hpp.
|
inline |
Same as WaitForNotification(ALIB_DBG_TAKE_CI), but takes a time span that defines a maximum wait time.
maxWaitTimeSpan | The maximum time to wait. |
ci | Caller information. Use macro ALIB_COMMA_CALLER_PRUNED with invocations. |
Definition at line 412 of file condition.hpp.
|
inline |
Same as WaitForNotification(ALIB_DBG_TAKE_CI), but takes a C++ time span that defines a maximum wait time.
maxWaitTimeSpan | The maximum time to wait. |
ci | Caller information. Use macro ALIB_COMMA_CALLER_PRUNED with invocations. |
Definition at line 376 of file condition.hpp.