Async++ unknown
Async (co_await/co_return) code for C++
Loading...
Searching...
No Matches
asyncpp::single_consumer_event Class Reference

Simple manual reset event supporting a single consumer. More...

#include <asyncpp/event.h>

Public Member Functions

constexpr single_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.
 

Detailed Description

Simple manual reset event supporting a single consumer.

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
single_consumer_event is threadsafe, however at most one coroutine can wait on it at any given time. Calling set(), reset() or is_set() from different threads at arbitrary times is fine, 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)

Constructor & Destructor Documentation

◆ single_consumer_event()

constexpr asyncpp::single_consumer_event::single_consumer_event ( bool set_initially = false)
inlineexplicitconstexprnoexcept

Construct a new event.

Parameters
set_initiallyThe initial state of the event (true => set, false => unset)

Member Function Documentation

◆ is_awaited()

bool asyncpp::single_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::single_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::single_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::single_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_dispatcherFallback 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::single_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_dispatcherThe dispatcher to resume on or nullptr to resume inside set()
Returns
Awaitable

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