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.

Implementation

The strand uses a service‐based architecture with a fixed pool of 211 implementation objects. New strands hash to select an impl from the pool. Strands that hash to the same index share serialization, which is harmless (just extra serialization) and rare with 211 buckets.

Executor Concept

This class satisfies the Executor concept, providing: ‐ context() ‐ Returns the underlying execution context ‐ on_work_started() / on_work_finished() ‐ Work tracking ‐ dispatch(h) ‐ May run immediately if strand is idle ‐ post(h) ‐ Always queues for later execution

Thread Safety

Distinct objects: Safe. Shared objects: Safe.

Example

thread_pool pool(4);
auto strand = make_strand(pool.get_executor());

// These coroutines will never run concurrently
strand.post(coro1);
strand.post(coro2);
strand.post(coro3);

Types

Name

Description

inner_executor_type

The type of the underlying executor.

Member Functions

Name

Description

strand [constructor]

Constructors

operator=

Assignment operators

context

Return the underlying execution context.

dispatch

Dispatch a coroutine 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 coroutine to the strand.

running_in_this_thread

Determine whether the strand is running in the current thread.

operator==

Compare two strands for equality.

Template Parameters

Name Description

E

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

See Also

make_strand, Executor

Created with MrDocs