template <typename Context = empty_context>
nxt::scope class

A structured concurrency scope.

A scope owns a stop source and links it to an optional parent stop token. It represents a bounded lifetime for work spawned inside a process.

Constructors, destructors, conversion operators

scope(nxt::scheduler& sched, Context context = {}) explicit
scope(nxt::scheduler& sched, std::stop_token parent_token, Context context = {})
scope(const scope&) deleted
scope(scope&&) deleted
~scope()

Public functions

template <typename... Awaitables>
auto all(Awaitables && ... awaitables) →  auto
Run all awaitables to completion within this scope.
auto all(std::vector<task<>> tasks) →  auto
Run stored void tasks to completion within this scope.
auto all()
Run all tasks previously spawned into this scope.
template <typename... Awaitables>
auto any(Awaitables && ... awaitables) →  auto
auto any(std::vector<task<>> tasks) →  auto
auto any()
void cancel()
Request cancellation of all operations bound to this scope.
auto cancelled() const →  bool noexcept
Check if cancellation has been requested.
void check() const
Throw cancelled{} if the scope has been cancelled.
auto context() →  Context& noexcept
Access the contextual capabilities carried by this scope.
auto context() const →  const Context& noexcept
auto operator=(const scope&) →  scope& deleted
auto operator=(scope&&) →  scope& deleted
auto scheduler() →  nxt::scheduler& noexcept
Access the scheduler.
void spawn(task<> t)
auto stop_source() →  std::stop_source& noexcept
Get the stop source (for libcoro's when_any).
auto stop_token() const →  std::stop_token noexcept
Get the stop token for manual checking or passing to other APIs.
auto subscope() →  scope
Create a child scope with the same context.
template <typename ChildContext>
auto subscope(ChildContext context) →  scope<ChildContext>
Create a child scope with a replacement context.

Function documentation

template <typename Context>
nxt::scope::scope(nxt::scheduler& sched, std::stop_token parent_token, Context context = {})

Create a scope with a parent stop token. When the parent is cancelled, this scope is also cancelled.

template <typename Context>
template <typename... Awaitables>
auto nxt::scope::any(Awaitables && ... awaitables)

Run awaitables until one completes, then request cancellation of this scope. Awaitables that should stop early must observe this scope's stop token.

template <typename Context>
auto nxt::scope::any(std::vector<task<>> tasks)

Run stored void tasks until one completes, then request cancellation of this scope.

template <typename Context>
auto nxt::scope::any()

Run previously spawned tasks until one completes, then request cancellation of this scope.

template <typename Context>
void nxt::scope::spawn(task<> t)

Add a void task to this scope. It starts when all() or any() is awaited.