Concurrent Composition
Headers: <boost/capy/when_all.hpp> and <boost/capy/when_any.hpp>. The async_waker used near the end comes from <boost/capy/ex/async_waker.hpp>.
Both combinators take children that are I/O awaitables whose result is a results, for example task<results<…>>. when_all returns a single results, and when_any returns a std::variant of the error code and the children’s payloads. If the results type is new, read The results Type first. Every result on this page is expressed in it.
Overview
Sequential execution—one task after another—is the default when using co_await:
capy::task<> sequential()
{
co_await task_a(); // Wait for A
co_await task_b(); // Then wait for B
co_await task_c(); // Then wait for C
}
Independent operations can run concurrently instead:
capy::task<> concurrent()
{
// Run A, B, C concurrently
co_await capy::when_all(task_a(), task_b(), task_c());
}
when_all: Wait for All Tasks
when_all starts multiple results-returning children concurrently and waits for all of them to complete. It returns task<results<R1, R2, …, Rn>>, a single ec plus the flattened payloads:
#include <boost/capy/when_all.hpp>
capy::task<capy::results<int>> fetch_a()
{
co_return capy::results<int>{std::error_code(), 1};
}
capy::task<capy::results<int>> fetch_b()
{
co_return capy::results<int>{std::error_code(), 2};
}
capy::task<capy::results<std::string>> fetch_c()
{
co_return capy::results<std::string>{std::error_code(), "hello"};
}
capy::task<> example()
{
auto [ec, a, b, c] = co_await capy::when_all(
fetch_a(), fetch_b(), fetch_c());
// ec == std::error_code{} (success)
// a == 1
// b == 2
// c == "hello"
}
Result Type
when_all returns results<R1, …, Rn> where each Ri is the child’s payload flattened: results<T> contributes T, results<> contributes tuple<>, and results<A, B> contributes tuple<A, B>. See when_all for the full contract.
Check ec first. Each value is still the payload its child produced, even when that child or a sibling reported an error. The range overload differs: once any child fails, it returns an empty vector.
Payload-free children
task<results<>> children contribute tuple<> to the result:
capy::task<capy::results<>> void_task() { co_return capy::results<>{}; }
capy::task<capy::results<int>> int_task()
{
co_return capy::results<int>{std::error_code(), 42};
}
capy::task<> example()
{
auto [ec, a, b, c] = co_await capy::when_all(
int_task(), void_task(), int_task());
// a == 42 (int)
// b == tuple<> (from task<results<>>)
// c == 42 (int)
}
When all children are task<results<>>, the result holds only the error code:
capy::task<> example()
{
auto r = co_await capy::when_all(void_task_a(), void_task_b());
if (std::get<0>(r))
{
// handle error
}
}
Error Handling
I/O errors are reported through the error code that leads the results. When any child returns a non-zero error code:
-
Stop is requested for sibling tasks. A child exception, or a stop request on the caller’s stop token, requests stop the same way.
-
All tasks complete (or respond to stop)
-
The first
ec(in completion order, not input order) is propagated in the outerresults
capy::task<> example()
{
auto [ec, a, b] = co_await capy::when_all(task_a(), task_b());
if (ec)
std::cerr << "Error: " << ec.message() << "\n";
}
If a task throws an exception, it is captured and rethrown after all tasks complete. Exceptions take priority over ec.
capy::task<capy::results<int>> might_throw(bool fail)
{
if (fail)
throw std::runtime_error("failed");
co_return capy::results<int>{std::error_code(), 42};
}
capy::task<> example()
{
try
{
co_await capy::when_all(might_throw(true), might_throw(false));
}
catch (std::runtime_error const& e)
{
// Catches the exception from the failing task
}
}
Stop Propagation
Well-behaved tasks should check their stop token and exit promptly:
capy::task<capy::results<>> long_running()
{
auto token = co_await capy::this_coro::stop_token;
for (int i = 0; i < 1000; ++i)
{
if (token.stop_requested())
co_return capy::results<>{}; // Exit early when sibling fails
co_await do_iteration();
}
co_return capy::results<>{};
}
when_any: First-to-Succeed Wins
when_any starts multiple results-returning children concurrently. It selects the first child that succeeds (!ec), requests stop on the others, and completes when all have finished:
#include <boost/capy/when_any.hpp>
capy::task<> example()
{
auto result = co_await capy::when_any(
fetch_int(), // task<results<int>>
fetch_string() // task<results<std::string>>
);
// result is std::variant<std::error_code, int, std::string>
// index 0: all tasks failed (error_code)
// index 1: fetch_int won
// index 2: fetch_string won
}
The result is a variant with error_code at index 0 (failure/no winner) and one alternative per input task at indices 1..N. When a winner is found, stop is requested for all siblings. All tasks complete before when_any returns.
When every task fails, when_any reports a failure, but which one is unspecified. The result either carries an error_code at index 0 or rethrows one of the children’s exceptions. Unlike when_all, there is no priority between error codes and exceptions. In particular, there is no guarantee that the reported failure is the first or last to complete.
Errors Do Not Win
A child that returns a non-zero ec (or throws) does not win, and it does not cancel its siblings. when_any keeps waiting until some child succeeds or until every child has finished. Only when all children fail does the result settle at index 0, holding an error_code, or rethrow a child’s exception.
If you need "complete on the first child to finish, success or error," that behavior is opt-in — wrap the child as shown below.
Treating an Error as a Win
To make a child win on an error, wrap it so the error becomes a success before when_any sees it.
The first pattern translates a specific, benign error into success. Other errors propagate unchanged, so they still do not win:
// canceled is benign here: translate it to success so when_any picks this child.
capy::task<capy::results<>> wrapped()
{
auto [ec] = co_await inner();
if (ec == capy::cond::canceled)
co_return capy::results<>{}; // success: when_any sees a winner
co_return capy::results<>{ec}; // propagate other errors unchanged
}
The second pattern lifts the inner ec into the payload. The wrapper always succeeds, so it wins on its first completion, carrying the original error code to the caller:
// Always succeeds; the winner's payload carries the original ec.
capy::task<capy::results<std::error_code>> wrapped()
{
auto [ec] = co_await inner();
co_return capy::results<std::error_code>{std::error_code(), ec};
}
// when_any(wrapped(), ...) -> variant<error_code, std::error_code, ...>
// index 0: every child failed
// index i: child i won; std::get<i>(result) is its original ec
Practical Patterns
Parallel Fetch
Fetch multiple resources concurrently:
capy::task<capy::results<page_data>> fetch_page_data(std::string url)
{
auto [ec, header, body, sidebar] = co_await capy::when_all(
fetch_header(url),
fetch_body(url),
fetch_sidebar(url)
);
if (ec)
co_return capy::results<page_data>{ec, {}};
co_return capy::results<page_data>{std::error_code(), {
std::move(header),
std::move(body),
std::move(sidebar)
}};
}
Fan-Out/Fan-In
Process items concurrently, then combine results using the range overload:
capy::task<capy::results<int>> process_item(item i);
capy::task<int> process_all(std::vector<item> items)
{
std::vector<capy::task<capy::results<int>>> tasks;
for (auto const& item : items)
tasks.push_back(process_item(item));
auto [ec, results] = co_await capy::when_all(std::move(tasks));
if (ec)
co_return 0;
int total = 0;
for (auto v : results)
total += v;
co_return total;
}
On a range of results<T> children, when_all yields results<std::vector<T>>, or results<> when the children carry no payload. when_any on a range yields a std::variant whose second alternative pairs the winner’s index with its payload, or holds only the index for payload-free children. Both throw std::invalid_argument on an empty range.
|
|
Racing a Deadline
when_any is the tool for expressing a deadline race directly. Two waker waits, one completed by the operation itself, the other by whatever plays the clock, race as ordinary when_any children:
capy::task<capy::results<std::string>> await_fetch(fetch_channel& ch)
{
auto [ec] = co_await ch.fetch_ready.wait();
if (ec)
{
ch.cancelled.store(true);
co_return capy::results<std::string>{ec, {}};
}
co_return capy::results<std::string>{std::error_code(), std::move(ch.result)};
}
capy::task<capy::results<>> deadline(capy::async_waker& waker)
{
auto [ec] = co_await waker.wait();
co_return capy::results<>{ec};
}
auto race = [&]() -> capy::task<>
{
auto result = co_await capy::when_any(
await_fetch(fetch_ch),
deadline(deadline_waker));
winner = result.index();
if (result.index() == 1)
fetch_result = std::get<1>(std::move(result));
};
The [&] capture is safe here only because the example blocks until the race finishes, so the lambda object outlives its coroutine. Lambda Coroutine Captures explains the hazard.
deadline_waker.wake() can come from any thread: for example a std::thread that sleeps for the deadline, a hardware event, or a completion callback from another library. See Timeout with Cancellation for the full example, including the fetch_channel type and both std::thread objects that play fetch worker and clock.
For timed I/O against a socket, prefer Corosio’s own timed operations: they schedule the deadline on the platform event loop instead of a dedicated thread.