A simple class which aggregates C++ standard library mechanics provided with std::promise
and std::future
into one simple interface.
The following features and facts are notable:
The class cannot be copied (just like std::promise
can't be). Therefore, usually a pointer to an object is passed to the fulfilling thread and the waiting thread is responsible for ensuring the lifecycle of the object survived until the promise is fulfilled.
With debug-compilations, the field DbgWaitTimeLimit enables the raise of ALib warnings in case a certain wait time is exceeded when using the unlimited blocking method
Furthermore, two ALib warnings may be raised with destruction:
Definition at line 57 of file promise.hpp.
#include <promise.hpp>
Public Type Index: | |
enum class | State { Unfulfilled , OK , Error , EmergencyStop , Custom } |
Public Field Index: | |
lang::CallerInfo | DbgFulfillCI |
lang::CallerInfo | DbgWaitCI |
Ticks::Duration | DbgWaitTimeLimit = Ticks::Duration::FromAbsoluteSeconds(2) |
Public Method Index: | |
Promise () | |
Default constructor. Sets the state to State::Unfulfilled. | |
~Promise () | |
Destructor. | |
void | DbgOmitDestructionWarning () |
void | Fulfill (const CallerInfo &ci, State state=State::OK) |
State | Wait (ALIB_DBG_TAKE_CI) |
State | WaitFor (const Ticks::Duration &maxWaitTimeSpan, const CallerInfo &ci) |
State | WaitFor (const Ticks::Duration::TDuration &maxWaitTimeSpan, const CallerInfo &ci) |
State | WaitUntil (const Ticks &wakeUpTime, const CallerInfo &ci) |
Protected Field Index: | |
std::future< State > | future |
Used for implementation. | |
std::promise< State > | promise |
Used for implementation. | |
|
strong |
Enumerates possible states. With construction, Unfulfilled is set. Error or a custom value could be used if the promise could not be fulfilled for any reason. EmergencyStop could be the right choice if the whole application should stop. But this is all up to the using code.
Definition at line 64 of file promise.hpp.
lang::CallerInfo DbgFulfillCI |
Debug-information about the first caller to Fulfill. A second (forbidden) call will be asserted with information about where the first invocation was made.
Definition at line 92 of file promise.hpp.
lang::CallerInfo DbgWaitCI |
Debug-information about the first caller to a successful wait. A second call will be asserted with information about where the first invocation to a successful wait was made.
Definition at line 97 of file promise.hpp.
Ticks::Duration DbgWaitTimeLimit = Ticks::Duration::FromAbsoluteSeconds(2) |
This is a threshold that causes the non-timed Wait method to raise a ALib warning in debug-builds in case a thread is blocked longer than the given duration.
To disable warnings in cases that high block times are suitable, set this value to 0
. The default value is two seconds.
Definition at line 87 of file promise.hpp.
|
protected |
Used for implementation.
Definition at line 78 of file promise.hpp.
|
protected |
Used for implementation.
Definition at line 77 of file promise.hpp.
|
inline |
Default constructor. Sets the state to State::Unfulfilled.
Definition at line 103 of file promise.hpp.
|
inline |
Destructor.
Definition at line 113 of file promise.hpp.
|
inline |
With debug-compilations, a warning is raised on destruction, in case either Fulfill was not called or a waiting method was not called (or both). With an invocation of this method, such warnings can be omitted.
Note that the function is available in release-builds as well, but empty and optimized-out.
Definition at line 125 of file promise.hpp.
|
inline |
This is to be invoked by the "fulfilling" thread which received this object, for example as a part of a command, to signal that the promise is considered fulfilled.
A thread waiting with methods Wait, WaitUntil, or WaitFor will be woken up.
ci | Caller information. Only available with debug-builds. Use macro ALIB_CALLER_PRUNED to pass this parameter, respectively. Use macro ALIB_CALLER_PRUNED_COMMA if state should be non-defaulted. |
state | The result to set. Defaults to OK. |
Definition at line 142 of file promise.hpp.
|
inline |
Waits an unlimited time for the promise to become fulfilled.
Definition at line 161 of file promise.hpp.
|
inline |
Waits for the promise to become fulfilled, but only for a given duration.
maxWaitTimeSpan | The maximum time to wait. |
ci | Caller information. Only available with debug-builds. Use macro ALIB_COMMA_CALLER_PRUNED to pass this parameter. |
Definition at line 234 of file promise.hpp.
|
inline |
Waits for the promise to become fulfilled, but only for a given duration.
maxWaitTimeSpan | The maximum time to wait. |
ci | Caller information. Only available with debug-builds. Use macro ALIB_COMMA_CALLER_PRUNED to pass this parameter. |
Definition at line 204 of file promise.hpp.
|
inline |
Waits for the promise to become fulfilled, but only until a given point in time.
wakeUpTime | The point in time to wake up, even if not notified. |
ci | Caller information. Only available with debug-builds. Use macro ALIB_COMMA_CALLER_PRUNED to pass this parameter. |
Definition at line 248 of file promise.hpp.