boost::capy::strand

Provides serialized coroutine execution for any executor type.

Synopsis

Declared in <boost/capy/ex/strand.hpp>

template<typename Executor>
class strand;

Description

A strand wraps an inner executor and ensures that coroutines dispatched through it never run concurrently. At most one coroutine executes at a time within a strand, even when the underlying executor runs on multiple threads.

Strands are lightweight handles that can be copied freely. Copies share the same internal serialization state, so coroutines dispatched through any copy are serialized with respect to all other copies.

Invariant

Coroutines resumed through a strand shall not run concurrently.

Executor Concept

This class satisfies the Executor concept, providing:

  • context() returns the underlying execution context.

  • on_work_started() and on_work_finished() track work.

  • dispatch(continuation&) may resume inline when already in this strand.

  • post(continuation&) always queues for later execution.

Coroutines queued on the strand when its context shuts down are unwound via coro_chain::reset rather than resumed.

Thread Safety

Distinct objects: Safe. Shared objects: Safe.

Example

void post_three(capy::thread_pool& pool,
    capy::coro_chain c1, capy::coro_chain c2, capy::coro_chain c3)
{
    capy::strand sd(pool.get_executor());  // CTAD deduces the executor type

    sd.post(std::move(c1));
    sd.post(std::move(c2));
    sd.post(std::move(c3));
}

Type Aliases

Name

Description

inner_executor_type

Names the executor type this strand<Ex> wraps.

Member Functions

Name

Description

strand [constructor]

Constructors

operator=

Assignment operators

context

Return the underlying execution context.

dispatch

Dispatch a continuation through the strand.

get_inner_executor

Return the underlying executor.

on_work_finished

Notify that work has finished.

on_work_started

Notify that work has started.

post

Post a continuation to the strand.

running_in_this_thread

Check whether the strand is running in the current thread.

operator==

Compare two strands for equality.

Deduction Guides

Name

Description

strand<Ex>

Deduce the executor type from the constructor argument.

Template Parameters

Name

Description

Ex

The type of the underlying executor. Must satisfy the Executor concept.

Preconditions

  • A strand holds only a non‐owning reference to its inner executor's execution context (for example a thread_pool). That context must outlive every post() and dispatch() call; posting or dispatching concurrently with, or after, the context's destruction is undefined behavior. To guarantee this, submit work through run_async or run. Their operations are work‐tracked, so the context's join() waits for them. Call join() on the context before destroying it, rather than posting to a strand from an external thread the context does not track. Destroying the strand handle itself is always safe, including after the context is destroyed.

See Also

Created with MrDocs