boost::capy::run_async
Bind an executor and separate result and error handlers 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,
class H2>
requires (detail::RunAsyncHandler<H1> && detail::RunAsyncHandler<H2>)
[[nodiscard]]
auto
run_async(
Ex ex,
H1 h1,
H2 h2);
Description
The handler h1 is called with the task's result on success. The handler h2 is called with the exception_ptr on failure. 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; }
// 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 separate_handlers_result = 0;
std::string separate_handlers_error;
void run_with_separate_handlers_demo( capy::any_executor ex )
{
capy::run_async(ex,
[](int result) {
separate_handlers_result = result; // the successful value arrives here
},
[](std::exception_ptr ep) {
try { std::rethrow_exception(ep); }
catch (std::exception const& e) {
separate_handlers_error = e.what(); // copied: the message outlives the 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 on success. |
h2 |
The handler to invoke with the exception on failure. |