boost::capy::run_async_wrapper
Installs the frame allocator, then starts the task on the executor when called once.
Synopsis
Declared in <boost/capy/ex/run_async.hpp>
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 |
-
Stored wrapper. Storing the wrapper itself (
auto w = run_async(ex);) compiles fine. C++17 guaranteed copy elision constructswdirectly from the prvalue. The deleted copy/move constructors are never considered. What the rvalue ref‐qualifier onoperator()rejects is calling through that stored lvalue:w(my_task())does not compile, andstd::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 beforerun_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_asyncruns. An example issubmit(ex, my_task()), wheresubmitcallsrun_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 |
|
Constructors |
|
Restore the previously installed frame allocator. |
Assignment operators |
|
Start the task for execution. |
Return Value
|
The return value should not be discarded. |
Template Parameters
Name |
Description |
Ex |
The executor type satisfying the |
Handlers |
The handler type (default_handler or handler_pair). |
Alloc |
The allocator type (value type or memory_resource*). |
See Also
Created with MrDocs