Simple manual reset event supporting multiple consumers.
More...
#include <asyncpp/event.h>
|
constexpr | multi_consumer_event (bool set_initially=false) noexcept |
| Construct a new event.
|
|
bool | is_set () const noexcept |
| Query if the event is currently set.
|
|
bool | is_awaited () const noexcept |
| Query if the event is currently being awaited.
|
|
bool | set (dispatcher *resume_dispatcher=nullptr) noexcept |
| Set the event.
|
|
void | reset () noexcept |
| Reset the event back to unset.
|
|
auto | operator co_await () noexcept |
| Suspend the current coroutine until the event is set.
|
|
constexpr auto | wait (dispatcher *resume_dispatcher=nullptr) noexcept |
| Suspend the current coroutine until the event is set.
|
|
Simple manual reset event supporting multiple consumers.
This is similar in concept to a std::condition_variable and allows synchronization between coroutines, as well as normal code and coroutines. If the current coroutine co_await's the event it is suspended until some other coroutine or thread calls set(). If the event is already set when calling co_await the coroutine will directly continue execution in the current thread. If the event is not set, the coroutine gets resumed on the dispatcher that's passed into wait() or inside the call to set() if no dispatcher was provided. The operator co_await will behave as if wait() was called with the result of dispatcher::current(), meaning the coroutine is resumed on the same dispatcher it suspended (not necessarily the same thread, e.g. on a thread pool). If no dispatcher is associated with the current thread it is resumed inside set().
- Note
- multi_consumer_event is threadsafe and multiple consumers can await it. Calling set() or is_set() from different threads at arbitrary times is safe, though it might be hard to predict the results.
-
Destroying a event thats currently being awaited can cause resource leaks as the waiting coroutine can never resume. (This asserts in debug mode)
◆ multi_consumer_event()
constexpr asyncpp::multi_consumer_event::multi_consumer_event |
( |
bool | set_initially = false | ) |
|
|
inlineexplicitconstexprnoexcept |
Construct a new event.
- Parameters
-
set_initially | The initial state of the event (true => set, false => unset) |
◆ is_awaited()
bool asyncpp::multi_consumer_event::is_awaited |
( |
| ) |
const |
|
inlinenoexcept |
Query if the event is currently being awaited.
- Note
- Do not base decisions on this value, as it might change at any time by a call to set()
◆ is_set()
bool asyncpp::multi_consumer_event::is_set |
( |
| ) |
const |
|
inlinenoexcept |
Query if the event is currently set.
- Note
- Do not base decisions on this value, as it might change at any time by a call to reset()
◆ operator co_await()
auto asyncpp::multi_consumer_event::operator co_await |
( |
| ) |
|
|
inlinenoexcept |
Suspend the current coroutine until the event is set.
If the event is already set, it resumes immediately on the current thread, otherwise the coroutine is suspended and waits until a call to set() is made. The coroutine will resume on the current dispatcher if the thread belongs to a dispatcher or inside set() if not.
- Returns
- Awaitable
◆ set()
bool asyncpp::multi_consumer_event::set |
( |
dispatcher * | resume_dispatcher = nullptr | ) |
|
|
inlinenoexcept |
Set the event.
- Note
- Depending on the way the event was awaited, the suspended coroutine might resume in this call.
- Parameters
-
resume_dispatcher | Fallback dispatcher to use for resuming the coroutine if none was passed to wait() |
- Returns
- true if a coroutine has been waiting and was resumed
◆ wait()
constexpr auto asyncpp::multi_consumer_event::wait |
( |
dispatcher * | resume_dispatcher = nullptr | ) |
|
|
inlineconstexprnoexcept |
Suspend the current coroutine until the event is set.
If the event is already set, it resumes immediately on the current thread, otherwise the coroutine is suspended and waits until a call to set() is made. The coroutine will resume on the provided dispatcher if not nullptr or inside set() if not.
- Parameters
-
resume_dispatcher | The dispatcher to resume on or nullptr to resume inside set() |
- Returns
- Awaitable
The documentation for this class was generated from the following file: