std::async


Keyword: std::async

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.

Key Concepts

Basic Example: Running a Function Asynchronously

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;
}

Explanation

Expected Output

Starting async task...
Doing other work in main thread...
Result from async task: 100

Explanation of Output

Using Launch Policies with std::async

Example with Deferred 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;
}

Expected Output with Deferred Execution

Starting async task with deferred execution...
Doing other work in main thread...
Result from deferred task: 100

Explanation of Output

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.

When to Use std::async

Conclusion

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++.

Previous Page | Course Schedule | Course Content