std::async
is a powerful feature in C++11 that allows you to run functions asynchronously, meaning that they can execute in parallel with other functions. It is part of the C++ Standard Library's thread support and is used to create tasks that can run concurrently, which can be particularly useful for improving the performance of your programs by utilizing multiple cores.
std::async
, it returns a std::future
object, which you can use to obtain the result of the function once it has finished executing.Here’s a simple example demonstrating how to use std::async to run a function asynchronously and retrieve its result:
#include <iostream>
#include <future>
#include <thread>
#include <chrono>
int longComputation(int x) {
std::this_thread::sleep_for(std::chrono::seconds(3)); // Simulate a long computation
return x * x;
}
int main() {
std::cout << "Starting async task..." << std::endl;
// Start the longComputation function asynchronously
std::future<int> result = std::async(std::launch::async, longComputation, 10);
std::cout << "Doing other work in main thread..." << std::endl;
// Get the result from the async task (this will block if the task is not finished)
int value = result.get();
std::cout << "Result from async task: " << value << std::endl;
return 0;
}
std::async(std::launch::async, longComputation, 10);
: This line starts the longComputation function asynchronously in a separate thread. The std::launch::async
policy ensures that the function is run asynchronously (in a separate thread immediately).std::future<int> result;
: The std::future object holds the result of the asynchronous operation. You can retrieve the result once the operation completes.result.get();
: This function waits for the asynchronous operation to complete and then retrieves the result. If the task is already complete, get()
returns the result immediately; otherwise, it blocks until the result is ready.Starting async task...
Doing other work in main thread...
Result from async task: 100
longComputation
function runs asynchronously.result.get()
.std::async
can be configured with different launch policies:std::launch::async
: Forces the function to execute asynchronously in a separate thread.std::launch::deferred
: The function call is deferred until the result is explicitly requested (i.e., when get()
or wait()
is called). The function will execute in the same thread that requests the result.
Default behavior: If you do not specify a launch policy, the behavior is implementation-defined. The implementation may choose to run the function asynchronously or defer its execution.#include <iostream>
#include <future>
#include <thread>
#include <chrono>
int longComputation(int x) {
std::this_thread::sleep_for(std::chrono::seconds(3)); // Simulate a long computation
return x * x;
}
int main() {
std::cout << "Starting async task with deferred execution..." << std::endl;
// Start the longComputation function with deferred execution
std::future<int> result = std::async(std::launch::deferred, longComputation, 10);
std::cout << "Doing other work in main thread..." << std::endl;
// The function is not executed until result.get() is called
int value = result.get();
std::cout << "Result from deferred task: " << value << std::endl;
return 0;
}
Starting async task with deferred execution...
Doing other work in main thread...
Result from deferred task: 100
In this example, the longComputation function does not start until result.get()
is called. Therefore, the main thread performs other work first, and the computation only begins when the result is requested.
std::async
to parallelize independent tasks in your program, leveraging multiple CPU cores.std::async
is a powerful and flexible tool in C++ that allows you to execute functions asynchronously, making it easier to write concurrent programs without directly managing threads. Whether you want to parallelize tasks, run long computations in the background, or simply defer execution, std::async provides a straightforward and safe way to achieve concurrency in modern C++.