dispatcher

A dispatcher is a callable object that accepts a coroutine handle and schedules it for resumption.

Requires: C++20

Synopsis

Defined in header <boost/capy/affine.hpp>

namespace boost::capy {

template<typename D, typename P = void>
concept dispatcher = requires(D const& d, std::coroutine_handle<P> h) {
    { d(h) } -> std::convertible_to<coro>;
};

} // namespace boost::capy

Description

A dispatcher encapsulates the rules for how and where a coroutine resumes. When invoked with a coroutine handle, the dispatcher schedules the handle for resumption and returns a handle suitable for symmetric transfer.

Dispatchers must be const-callable, enabling thread-safe concurrent dispatch from multiple coroutines. The dispatcher may resume the handle inline (by returning the handle itself) or queue it for later execution (by returning std::noop_coroutine()).

Since coro (an alias for std::coroutine_handle<void>) has operator() which invokes resume(), the handle itself is callable and can be dispatched directly.

Valid Expressions

Given:

  • d — a const value of type D

  • h — a value of type std::coroutine_handle<P>

Expression Return Type Description

d(h)

convertible to coro

Schedule h for resumption and return a handle for symmetric transfer

Example

#include <boost/capy/affine.hpp>

using boost::capy::coro;
using boost::capy::dispatcher;

struct inline_dispatcher
{
    coro operator()(coro h) const
    {
        return h;  // Resume inline via symmetric transfer
    }
};

struct queuing_dispatcher
{
    work_queue* queue_;

    coro operator()(coro h) const
    {
        queue_->push(h);
        return std::noop_coroutine();  // Caller returns to event loop
    }
};

static_assert(dispatcher<inline_dispatcher>);
static_assert(dispatcher<queuing_dispatcher>);

See Also