boost::capy::when_all

Execute multiple awaitables concurrently and collect their results.

Synopsis

Declared in <boost/capy/when_all.hpp>

template<IoAwaitable... As>
[[nodiscard]]
task</* implementation-defined */>
when_all(As... awaitables);

Description

Launches all awaitables simultaneously and waits for all to complete before returning. Results are collected in input order. If any awaitable throws, cancellation is requested for siblings and the first exception is rethrown after all awaitables complete.

  • All child awaitables run concurrently on the caller's executor

  • Results are returned as a tuple in input order

  • Void‐returning awaitables do not contribute to the result tuple

  • If all awaitables return void, `when_all` returns `task<void>`

  • First exception wins; subsequent exceptions are discarded

  • Stop is requested for siblings on first error

  • Completes only after all children have finished

Thread Safety

The returned task must be awaited from a single execution context. Child awaitables execute concurrently but complete through the caller's executor.

Example

task<> example()
{
    // Concurrent fetch, results collected in order
    auto [user, posts] = co_await when_all(
        fetch_user( id ),      // task<User>
        fetch_posts( id )      // task<std::vector<Post>>
    );

    // Void awaitables don't contribute to result
    co_await when_all(
        log_event( "start" ),  // task<void>
        notify_user( id )      // task<void>
    );
    // Returns task<void>, no result tuple
}

Return Value

A task yielding a tuple of non‐void results. Returns task<void> when all input awaitables return void.

Parameters

Name Description

awaitables

The awaitables to execute concurrently. Each must satisfy IoAwaitable and is consumed (moved‐from) when when_all is awaited.

See Also

IoAwaitable, task

Created with MrDocs