Defined in <stlab/concurrency/future.hpp>
template <typename E, typename F, typename...Ts>
auto when_all(E e, F f, future<Ts>... args)
(1)
template <typename E, typename F, typename I>
auto when_all(E e, F f, std::pair<I,I> range)
(2)

Creates a joining future

  1. This function create a joining future. When all passed args futures are fulfilled, then the continuation tasks defined with f is scheduled on the executor e.

  2. This function create a joining future out of the given range. In case that it is a range of futures of move-only types, the futures are moved internally out of the range into the function.

Parameters

e

Executor which is used to schedule the resulting task

f

Callable object that implements the task

args

Futures that shall be joined

range

a range of futures of type U, specified by a half open range. All futures must succeed, before the continuation is triggered. It takes a vector<U> filled with all results as result.

Return value

a future that joins all passed arguments and passes them to the associated function object

Example 1

#include <iostream>
#include <string>
#include <thread>
#include <stlab/concurrency/default_executor.hpp>
#include <stlab/concurrency/future.hpp>

using namespace std;
using namespace stlab;

int main() {
    auto argument1 = async(default_executor, [] { return 42; });
    auto argument2 = async(default_executor, [] { return string("The answer is"); });

    auto result = when_all(default_executor, [](int answer, std::string text) {
        cout << text << " " << answer << '\n';
    }, argument1, argument2);

    // Waiting just for illustration purpose
    while (!result.get_try()) { this_thread::sleep_for(chrono::milliseconds(1)); }
}

/*
    Result:

        The answer is 42
*/

Example 2

#include <iostream>
#include <string>
#include <thread>
#include <stlab/concurrency/default_executor.hpp>
#include <stlab/concurrency/immediate_executor.hpp>
#include <stlab/concurrency/future.hpp>
#include <stlab/concurrency/utility.hpp>

using namespace std;
using namespace stlab;

int main() {
    auto fv = []{
        return stlab::make_ready_future(immediate_executor);
    };
    auto fi = []{
        return stlab::make_ready_future<int>(42, immediate_executor);
    };
    auto fs = []{
        return stlab::make_ready_future<std::string>("Hello, world!", immediate_executor);
    };
    auto fb = []{
        return stlab::make_ready_future<bool>(true, immediate_executor);
    };

    auto f = when_all(stlab::default_executor, [](auto... args){
        for_each_argument([](auto x) { cout << x << "\n"; }, args...);
    }, fi(), fv(), fs(), fv(), fb(), fv());

    while (!f.get_try()) std::this_thread::sleep_for(std::chrono::milliseconds(1));
}

/*
    Result:

        42
        Hello, world!
        1
    or:
        1
        Hello, world!
        42
*/