Skip to content
This repository was archived by the owner on Oct 10, 2023. It is now read-only.

unpause::async::task

james h edited this page Aug 12, 2017 · 4 revisions

Task

template<class R, class... Args>
struct task : public detail::task_container

Container that holds a task that can be executed asynchronously. It is possible for tasks to return values, but for practical purposes generally you would use task::after to retrieve the result.

Member Functions Ctor

task::task(R&& r, Args&&... args);

Running the task

task::operator();
task::run_v();

Types

// function<void(R&)>
using after_type = typename detail::task_after<result_type>::function_type; 

Member Variables

// holds the function
std::function<result_type (Args...)> func; 
// stores arguments
std::tuple<Args...> args; 
// Run after the execution of the task, result is passed as the R parameter
after_type after; 

Utility Methods

template<class R, class... Args>
inline task<R,Args...> make_task(R&& r, Args&&... args)

Construct and return a task based on a lambda or other function type.


Example

int val = 10;
auto t = make_task([](int& val) { std::cout << "val is " << val << std::endl; }, val);
t();

Result:

val is 10

task::after is run after execution of the task and takes the result of the task as the parameter. For example:

int val = 10;
auto t = make_task([](int& val) { return val * 2; }, val);
t.after = [](int val) { std::cout << "val is " << val << std::endl; };
t();

Result:

val is 20.
Clone this wiki locally