What is std::async?
std::async
is a function template in C++ introduced by the C++ standard in the <future>
header as part of the thread support library from C++11. It is used to run a function asynchronously, allowing the main thread (or other threads) to continue running concurrently.
In summary, std::async
is a powerful tool for asynchronous programming in C++, making it easier to run tasks in parallel and manage their results efficiently.
Launching an asynchronous task
To execute a function asynchronously using std::async
, we can use the same approaches we used when starting threads in Chapter 3, with the different callable objects.
One approach is using a function pointer:
void func() { std::cout << "Using function pointer\n"; } auto fut1 = std::async(func);
Another approach is using a lambda function:
auto lambda_func = []() { std::cout << "Using lambda function\n"; }; auto...