boost::capy::run_async_wrapper

Installs the frame allocator, then starts the task on the executor when called once.

Synopsis

template<
    Executor Ex,
    class Handlers,
    class Alloc>
class [[nodiscard]] run_async_wrapper;

Description

This wrapper holds the run_async_trampoline coroutine, executor, stop token, and handlers. The run_async_trampoline is allocated when the wrapper is constructed (before the task due to C++17 postfix evaluation order).

The rvalue ref‐qualifier on operator() ensures the wrapper can only be used as a temporary, preventing misuse that would violate LIFO ordering.

Thread Safety

The wrapper itself should only be used from one thread. The handlers may be invoked from any thread where the executor schedules work.

**Always construct the task as the direct argument of the two‐call expression run_async(ex)(task).** The wrapper's constructor installs the frame allocator in thread‐local storage. The task's operator new reads that thread‐local state. Splitting the two calls apart in any of the following ways allocates the task's coroutine frame under the wrong allocator. Each does so silently, with no compile error.

  • Stored wrapper. Storing the wrapper itself (auto w = run_async(ex);) compiles fine. C++17 guaranteed copy elision constructs w directly from the prvalue. The deleted copy/move constructors are never considered. What the rvalue ref‐qualifier on operator() rejects is calling through that stored lvalue: w(my_task()) does not compile, and std::move(w)(my_task()) is required instead. The silent variant is storing the task (auto t = my_task(); run_async(ex)(std::move(t));): t's frame is allocated before run_async(ex) ever runs.

  • Preconstructed task. Passing an already‐constructed task object has the same effect as the stored‐wrapper case. So does passing a moved‐from local, or a task returned from an earlier statement. The frame exists before the allocator is installed.

  • Wrapper function. Forwarding the task through a helper that itself performs the two‐call pattern constructs the task as an argument to the helper. It is therefore constructed before the helper's body runs, and so before run_async runs. An example is submit(ex, my_task()), where submit calls run_async(ex)(std::forward<Task>(t)) internally.

See the Frame Allocators guide (doc/modules/ROOT/pages/4.coroutines/4g.allocators.adoc) for the full C++17‐evaluation‐order rationale behind this constraint.

Example

capy::task<void> my_task() { co_return; }

void correct_usage( capy::any_executor ex )
{
    // Correct usage - wrapper is temporary, task is the direct argument
    capy::run_async(ex)(my_task());
}

void rvalue_only_call( capy::any_executor ex )
{
    // Compiles - copy elision constructs w directly from the prvalue
    auto w = capy::run_async(ex);

    // Calling on the rvalue is the supported form; calling through
    // the stored lvalue is rejected by the rvalue ref-qualifier.
    using wrapper = decltype(w);
    static_assert(   std::is_invocable_v< wrapper,  capy::task<void> > );
    static_assert( ! std::is_invocable_v< wrapper&, capy::task<void> > );

    std::move(w)(my_task());  // Compiles: w is now an rvalue
}

void silent_misuse( capy::any_executor ex )
{
    // Compiles, but WRONG - task frame allocated before run_async runs
    auto t = my_task();
    capy::run_async(ex)(std::move(t));
}

Member Functions

Name

Description

run_async_wrapper [constructor]

Constructors

~run_async_wrapper [destructor]

Restore the previously installed frame allocator.

operator=

Assignment operators

operator()

Start the task for execution.

Return Value

The return value should not be discarded.

Template Parameters

Name

Description

Ex

The executor type satisfying the Executor concept.

Handlers

The handler type (default_handler or handler_pair).

Alloc

The allocator type (value type or memory_resource*).

See Also

Created with MrDocs