Friday, May 12, 2023

C++ - std::future and std::promise in C++

std::future and std::promise

In C++, std::future and std::promise are classes used for asynchronous communication between two threads. They are part of the standard library and can be used for various purposes such as implementing thread-safe data structures or handling parallel computations.

std::future is a class template that represents a value or an exception that will be available in the future. It can be used to wait for the result of an asynchronous operation or to retrieve the value of a function that is being executed in another thread.

std::promise is another class template that allows one thread to store a value or an exception that can be retrieved by another thread later. It is used to communicate the result of an asynchronous operation to another thread.

The basic idea behind std::future and std::promise is that one thread (usually the main thread) creates a std::promise object and passes it to another thread (the worker thread) along with a task to be executed. The worker thread executes the task and sets the value of the promise using the set_value() method. Meanwhile, the main thread waits for the result by calling the get() method on a corresponding std::future object.

Here's a simple example that demonstrates the use of std::future and std::promise in C++:

#include <iostream> #include <future> int main() { std::promise<int> promiseObj; std::future<int> futureObj = promiseObj.get_future(); // Start a new thread to execute a task std::thread t([&promiseObj]() { // Simulate some long-running task std::this_thread::sleep_for(std::chrono::seconds(2)); int result = 42; // Set the value of the promise promiseObj.set_value(result); }); // Wait for the result and print it int result = futureObj.get(); std::cout << "Result: " << result << std::endl; // Join the thread and exit t.join(); return 0; }

std::function, std::promise, and std::future

In this example, we create a std::promise object and a corresponding std::future object. We pass the std::promise object to a new thread along with a task to be executed. The new thread executes the task and sets the value of the promise using the set_value() method. Meanwhile, the main thread waits for the result by calling the get() method on the std::future object. Once the result is available, it is printed to the console.

example of how to use std::function, std::promise, and std::future together to implement a callback function:

#include <iostream> #include <functional> #include <future> void longRunningTask(std::function<void(int)> callback) { // Simulate a long-running task std::this_thread::sleep_for(std::chrono::seconds(3)); // Call the callback with the result of the task callback(42); } int main() { std::promise<int> promise; std::future<int> future = promise.get_future(); std::function<void(int)> callback = [&](int result) { promise.set_value(result); }; // Start the long-running task with the callback function longRunningTask(callback); // Wait for the result from the future int result = future.get(); std::cout << "Result: " << result << std::endl; return 0; }

In this example, the longRunningTask() function simulates a long-running task that takes 3 seconds to complete. The function takes a std::function<void(int)> parameter, which is a callback function that will be called with the result of the task.

In, we create a std::promise<int> and get a std::future<int> from it. We also create a std::function<void(int)> callback function that captures the promise by reference and sets its value when called with the result.

We then call longRunningTask() with the callback function, and wait for the result from the future using future.get(). Finally, we print the result to the console.

This example demonstrates how std::function, std::promise, and std::future can be used together to implement a callback function that receives a result from a long-running task.


No comments:

Post a Comment

LeetCode C++ Cheat Sheet June

🎯 Core Patterns & Representative Questions 1. Arrays & Hashing Two Sum – hash map → O(n) Contains Duplicate , Product of A...