stlab::async

template <typename E, typename F, typename ...Args>
future<std::result_of_t<F (Args...)>> async(E executor, F&& f, Args&&... args)
(1)
  1. The template function async runs the function f asynchronously on the specified executor and returns a stlab::future that will eventually hold the result of that function call.

Parameters

executor

The passed function will run on this executor

f

Callable object to call

args

parameters to pass to f

Return value

stlab::future referring to the shared state created by this call to std::async.

Example 1

#include <iostream>
#include <thread>

#include <stlab/concurrency/default_executor.hpp>
#include <stlab/concurrency/future.hpp>

using namespace std;
using namespace stlab;

int main() {
    auto f = async(default_executor, [] { return 42; });
 
    // Waiting just for illustrational purpose
    while (!f.get_try()) { this_thread::sleep_for(chrono::milliseconds(1)); }

    cout << "The answer is " << f.get_try().value() << "\n";
}

/*
    Result:

        The answer is 42
*/