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.
Executor Concept
This class satisfies the Executor concept, providing:
-
context()returns the underlying execution context. -
on_work_started()andon_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.
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));
}
Member Functions
Name |
Description |
|
Constructors |
Assignment operators |
|
Return the underlying execution context. |
|
Dispatch a continuation through the strand. |
|
Return the underlying executor. |
|
Notify that work has finished. |
|
Notify that work has started. |
|
Post a continuation to the strand. |
|
Check whether the strand is running in the current thread. |
|
Compare two strands for equality. |
Template Parameters
Name |
Description |
Ex |
The type of the underlying executor. Must satisfy the |
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 throughrun_asyncorrun. Their operations are work‐tracked, so the context'sjoin()waits for them. Calljoin()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.