Sobes.tech
Middle

What is std::future in C++?

sobes.tech AI

Answer from AI

std::future is a template class in C++ representing the result (value or exception) of an asynchronous operation that will become available in the future. It is used to obtain the result of a task executed in a separate thread or via std::async.

Key features:

  • Asynchrony: Allows performing an operation without blocking the current thread.
  • Getting the result: The get() method allows retrieving the result. If the result is not ready, get() will block the current thread until it is ready or an exception is thrown.
  • Data transfer: It is linked with a std::promise object, which provides a way to set the result or exception.
  • State: std::future has a state indicating whether the result is ready. You can check this with wait() or wait_for().
  • Single retrieval: The get() method can only be called once for each std::future object.
  • Result type: It is typed with the return type of the asynchronous operation.

Example using std::async:

#include <future>
#include <thread>
#include <chrono>
#include <iostream>

int calculate_something() {
    std::this_thread::sleep_for(std::chrono::seconds(1));
    return 42;
}

// Example of using std::future with std::async
int main() {
    // Launch an asynchronous operation
    std::future<int> result_future = std::async(std::launch::async, calculate_something);

    // Do other work while the operation is in progress
    std::cout << "Doing something else..." << std::endl;

    // Get the result (blocks if not ready)
    int result = result_future.get();
    std::cout << "Result is: " << result << std::endl;

    return 0;
}

Example using std::promise:

#include <future>
#include <thread>
#include <iostream>

void worker_function(std::promise<int> promise_obj) {
    std::this_thread::sleep_for(std::chrono::seconds(1));
    // Set the result
    promise_obj.set_value(123);
}

// Example of using std::future with std::promise
int main() {
    // Create promise and future
    std::promise<int> promise_obj;
    std::future<int> future_obj = promise_obj.get_future();

    // Launch worker in a separate thread, passing the promise
    std::thread worker_thread(worker_function, std::move(promise_obj));

    // Do other work while worker runs
    std::cout << "Doing something else..." << std::endl;

    // Get the result (blocks if not ready)
    int result = future_obj.get();
    std::cout << "Result from worker: " << result << std::endl;

    // Wait for the worker thread to finish
    worker_thread.join();

    return 0;
}

Important methods of std::future:

Method Description
get() Retrieves the result. Blocks if the result is not yet available. Can only be called once.
valid() Checks whether the std::future is associated with an asynchronous operation.
wait() Blocks the current thread until the result is available (or an exception is thrown).
wait_for() Waits for the result for a specified duration. Returns the wait status.
wait_until() Waits for the result until a specified point in time. Returns the wait status.

std::future is part of the <future> library and provides a convenient way to manage the results of asynchronous computations, simplifying multithreaded application development.