std::execution::when_all
| Defined in header <execution>
|
||
struct when_all_t { /* unspecified */ };
|
(1) | (since C++26) |
inline constexpr when_all_t when_all{};
|
(2) | (since C++26) (customization point object) |
| Call signature |
||
execution::sender auto when_all( execution::sender auto... sndrs );
|
(3) | (since C++26) |
Runs several senders concurrently and completes when all of them have completed. If they all succeed, the returned sender completes with the values of all of them, concatenated in the order the senders were passed. If one of them fails or is cancelled, the others are asked to stop, and the returned sender completes with that failure or cancellation once every input has finished.
when_all is the sender counterpart of "start these operations, then wait for all of them", and the usual way to fan out independent asynchronous work and join the results. Unlike this_thread::sync_wait, it does not block: the result is itself a sender, so the next step is attached with then or similar.
when_all only accepts senders that complete with a single set of values. For senders that can complete with several different sets of values, use when_all_with_variant.
Parameters
| sndrs | - | the senders to run concurrently; at least one, and each must complete with at most one set of values |
Return value
A sender that is started by starting all of sndrs..., and that completes when every one of them has completed:
- If all inputs complete with values, the returned sender completes with all the values, in the order of the arguments. An input that sends no values contributes nothing, and an input that sends several contributes all of them. For example
when_all(just(1, 2), just(), just(3))completes with1, 2, 3. - If an input completes with an error, the returned sender completes with that error. If several inputs fail, the first error to arrive is used. An exception thrown while copying an input's values is an error too (std::exception_ptr).
- If an input completes with the stopped signal and none failed, the returned sender completes stopped.
In both the error and the stopped case, the returned sender completes only after all inputs have completed: the other inputs are not abandoned, they are asked to stop and then waited for.
when_all(sndrs...) is ill-formed if no senders are passed or one of them does not satisfy execution::sender. It is also diagnosed at compile time if some input can complete with more than one set of values.
Customization point objects
The name execution::when_all denotes a customization point object, which is a const function object of a literal semiregular class type. The cv-unqualified version of its type is a tag type denoted as execution::when_all_t. See CustomizationPointObject for details.
Details
- Nothing runs when the sender is built. The inputs are started, one after another in argument order, when the returned sender is started. An input that completes asynchronously on another thread runs concurrently with the rest; an input that completes immediately (for example just) completes before the next one is started.
- The returned sender completes on the execution agent on which the last input completed, inline in that completion. If stop was already requested before the sender was started, it completes inside the call to start. It does not move work to another thread: to continue somewhere specific, use continues_on.
- Each input is started with an environment in which the stop token is replaced by one owned by
when_all. That token reports a stop request when the stop token of the receiverwhen_allis connected to does, and also when one of the inputs fails or is cancelled. Inputs that honor their stop token therefore finish early once a sibling has failed; inputs that ignore it keep running to the end, andwhen_allwaits for them. All other queries in the environment are forwarded unchanged. - The input values are copied or moved into the operation as they arrive (move-only values are fine), and moved out into the result.
Notes
- The returned sender does not report a completion scheduler, since the last input to finish can be on any execution resource. Follow it with continues_on if later steps must run on a known scheduler.
- Failure of one input does not skip the others: they are cancelled only if they check their stop token. For inputs that do work on a thread without checking stop, the cost of a failure is waiting for them to finish.
- To combine senders and avoid the single-set-of-values restriction, use when_all_with_variant.
Example
Two independent lookups run on two threads at the same time, and then receives both results, in argument order, when the slower one is done. Nothing runs until sync_wait starts the sender.
The standard header is not available in the online compilers yet, so this link runs the example on Compiler Explorer with stdexec, the reference implementation.
#include <execution>
#include <format>
#include <print>
#include <string>
#include <thread>
namespace ex = std::execution;
int main()
{
ex::run_loop loop_a, loop_b;
std::jthread thread_a([&] { loop_a.run(); });
std::jthread thread_b([&] { loop_b.run(); });
// Stand-ins for two independent asynchronous lookups.
auto load_name = ex::schedule(loop_a.get_scheduler())
| ex::then([] { return std::string("alice"); });
auto load_age = ex::schedule(loop_b.get_scheduler())
| ex::then([] { return 30; });
auto sndr = ex::when_all(load_name, load_age)
| ex::then([](std::string name, int age)
{
return std::format("{} is {} years old", name, age);
});
auto [text] = std::this_thread::sync_wait(std::move(sndr)).value();
std::println("{}", text);
loop_a.finish();
loop_b.finish();
}
Output:
alice is 30 years old
See also
| adapts multiple input senders, with each possibly having multiple completion signatures, into a sender that completes once all of the input senders have completed (customization point object) | |
(C++26) |
chains a function onto a sender, calling it with the values the sender sends and sending its result on (customization point object) |
(C++26) |
adapts a provided sender into one that completes on the provided scheduler's execution resource (customization point object) |