boost::capy::run_async
Bind an executor and a result handler to produce a launcher. Invoke the launcher with a task to start it.
Synopsis
Declared in <boost/capy/ex/run_async.hpp>
template<
Executor Ex,
class H1>
requires detail::RunAsyncHandler<H1>
[[nodiscard]]
auto
run_async(
Ex ex,
H1 h1);
Description
The handler h1 is called with the task's result on success. If h1 is also invocable with std::exception_ptr, it handles exceptions too. Otherwise, an unhandled exception calls std::terminate. If the task is abandoned before it completes, as a context's teardown does, no handler is called; the handlers are destroyed with it.
Construct the task as the direct argument of the two‐call expression run_async(ex)(task).
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.
Example
capy::task<int> compute_value() { co_return 42; }
template<class... Fs>
struct overloaded : Fs... { using Fs::operator()...; };
template<class... Fs>
overloaded(Fs...) -> overloaded<Fs...>;
// The handlers may run after this function returns, on whichever thread
// the executor schedules them, so the state they write must outlive
// the call.
int last_result = 0;
int last_result2 = 0;
bool last_failed = false;
void run_with_result_handler_demo( capy::any_executor ex )
{
capy::run_async(ex, [](int result) {
last_result = result; // the successful value arrives here
})(compute_value());
// Overloaded handler for both result and exception
overloaded handle_result_or_exception{
[](int result) { last_result2 = result; }, // the successful value arrives here
[](std::exception_ptr) { last_failed = true; } // the failure arrives here
};
capy::run_async(ex, handle_result_or_exception)(compute_value());
}
Return Value
A wrapper that accepts a task<T> for immediate execution.
|
The return value should not be discarded. |
Parameters
Name |
Description |
ex |
The executor to execute the task on. |
h1 |
The handler to invoke with the result (and optionally exception). |