stlab::package

template <typename Sig, typename E, typename F>
auto package(E executor, F f) ->
    std::pair<detail::packaged_task_from_signature_t<Sig>,
              future<detail::result_of_t_<Sig>>>
(1)
  1. The template function package creates a pair of a packaged_task and a future.

Parameters

executor

The passed function will run on this executor

f

Callable object to call

Return value

A std::pair of a packaged_task and the associated future.

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 p = package<int(int)>(default_executor, [](int x) { return x+x; });
    auto packagedTask = p.first;
    auto f = p.second;

    packagedTask(21);

    // 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
*/