std::async

From cppreference.com
 
 
Thread support library
Threads
thread (C++11)
this_thread namespace
get_id (C++11)
yield (C++11)
sleep_for (C++11)
sleep_until (C++11)
Mutual exclusion
mutex (C++11)
timed_mutex (C++11)
Generic lock management
lock_guard (C++11)
unique_lock (C++11)
defer_lock_t
try_to_lock_t
adopt_lock_t
(C++11)
(C++11)
(C++11)
lock (C++11)
try_lock (C++11)
defer_lock
try_to_lock
adopt_lock
(C++11)
(C++11)
(C++11)
Condition variables
condition_variable (C++11)
condition_variable_any (C++11)
notify_all_at_thread_exit (C++11)
cv_status (C++11)
Futures
promise (C++11)
future (C++11)
shared_future (C++11)
packaged_task (C++11)
async (C++11)
launch (C++11)
future_status (C++11)
future_error (C++11)
future_category (C++11)
future_errc (C++11)
 
Defined in header <future>
template< class Function, class... Args>

std::future<typename std::result_of<Function(Args...)>::type>

    async( Function&& f, Args&&... args );
(1) (since C++11)
template< class Function, class... Args >

std::future<typename std::result_of<Function(Args...)>::type>

    async( std::launch policy, Function&& f, Args&&... args );
(2) (since C++11)

The template function async provides the mechanisms for asynchronous function execution and for lazy evaluation of functions.

1) behaves the same as async(std::launch::async | std::launch::deferred, f, args...)

2) If policy & std::launch::async != 0 (the async bit is set), spawns a new thread of execution as if by std::thread(f, args...), except that if the function f returns a value or throws an exception, it is stored in the shared state accessible through the std::future that async returns to the caller.

If policy & std::launch::deferred != 0 (the deferred bit is set), converts args... the same way as by std::thread constructor, but does not spawn a new thread of execution. Instead, lazy evaluation is performed: the first call to a non-timed wait function on the std::future that async returned to the caller will cause f(args...) to be executed in the current thread. All further accesses to the same std::future will return the result immediately.

If both std::launch::async and std::launch::deferred bits are set in policy, it is up to the implementation whether to perform asynchronous execution or lazy evaluation.

Contents

[edit] Parameters

f - function or function object to call
args... - parameters to pass to f
policy - bitmask value, where individual bits control the allowed methods of execution
Bit Explanation
std::launch::async enable asynchronous evaluation
std::launch::deferred enable lazy evaluation


[edit] Return value

std::future referring to the return value of the function.

[edit] Exceptions

Throws std::system_error with error condition std::errc::resource_unavailable_try_again if the launch policy is std::launch::async and the implementation is unable to start a new thread.

[edit] Example

#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <future>
 
template<typename RAIter>
int parallel_sum(RAIter beg, RAIter end)
{
    typename RAIter::difference_type len = end-beg;
    if(len < 1000)
        return std::accumulate(beg, end, 0);
 
    RAIter mid = beg + len/2;
    auto handle = std::async(std::launch::async,
                              parallel_sum<RAIter>, mid, end);
    int sum = parallel_sum(beg, mid);
    return sum + handle.get();
}
int main()
{
    std::vector<int> v(10000, 1);
    std::cout << "The sum is " << parallel_sum(v.begin(), v.end()) << '\n';
}

Output:

The sum is 10000