All Modules Pages
The simplest way for asynchronous execution - std::async


std::async executes some piece of code in the background, possibly within a separate thread. Access to results (and waiting for thread execution) is possible via std::future.


Asssume you have one or more time consuming calculations finally providing some result:

SomeResult CalculateSomething(int in_val)
return ...

Then you can simply execute them asynchronously and in parallel by calling them via std::async. The result can be accesssed by use of a future.

// Start 2 calculations in parallel
std::future<SomeResult> calc1 = std::async(&CalculateSomething,17);
std::future<SomeResult> calc2 = std::async(&CalculateSomething,42);
// Do something else
// Wait for the results
SomeResult result1 = calc1.get();
SomeResult result2 = calc2.get();


std::async - most used features

You can provide an explicit start policy as first constructor argument for std::async:

Start policy Description
std::launch::async start separate execution thread
std::launch::deferred start calculation in the same thread at the moment when result is requested via the future (lazy evaluation)

For more info see std::async - complete reference at