Troubleshooting std::future_error: A Practical Guide

2025-09-08

The std::future_error exception is thrown when an operation on a std::future or std::shared_future fails. This typically happens because the associated state of the promise or task has been lost or is no longer valid. In simple terms, a std::future acts as a proxy for a value that will be computed in the future, and this error means that the connection to that future value has been broken.

The exception comes with an associated std::future_errc code that provides more specific information about the error. The most common error codes are

std::future_errc::broken_promise
The std::promise or std::packaged_task that was supposed to fulfill the future's value was destroyed before setting a value or an exception. This is a very common scenario.

std::future_errc::future_already_retrieved
You've tried to retrieve the result from a std::future more than once by calling get(). Remember, a std::future is a one-shot deal!

std::future_errc::no_state
This indicates that the std::future object itself doesn't have an associated shared state, perhaps because it was moved from or was created with a default constructor.

Let's look at some common mistakes and how to fix them.

This is perhaps the most frequent cause of std::future_error. It happens when the std::promise or std::packaged_task goes out of scope and is destroyed before the result can be set.

Problematic Code Example

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

void do_something_async(std::promise<int> p) {
    // Oops, we don't set the value!
    // The promise is destroyed when the function exits.
}

int main() {
    std::promise<int> p;
    std::future<int> f = p.get_future();
    
    std::thread t(do_something_async, std::move(p));
    
    try {
        int result = f.get(); // Throws std::future_error with broken_promise
        std::cout << "Result: " << result << std::endl;
    } catch (const std::future_error& e) {
        std::cout << "Caught a future_error: " << e.what() << std::endl;
        if (e.code() == std::future_errc::broken_promise) {
            std::cout << "Reason: The promise was destroyed." << std::endl;
        }
    }
    
    t.join();
    
    return 0;
}

Solution
Always make sure the std::promise sets a value or an exception before it is destroyed.

Corrected Code Example

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

void do_something_async_fixed(std::promise<int> p) {
    try {
        std::this_thread::sleep_for(std::chrono::seconds(1));
        p.set_value(42); //  The value is set successfully!
    } catch (...) {
        // If an error happens, set an exception
        p.set_exception(std::current_exception());
    }
}

int main() {
    std::promise<int> p;
    std::future<int> f = p.get_future();
    
    std::thread t(do_something_async_fixed, std::move(p));
    
    try {
        int result = f.get(); // Now this will wait and get the value
        std::cout << "Result: " << result << std::endl;
    } catch (const std::future_error& e) {
        std::cout << "Caught a future_error: " << e.what() << std::endl;
    }
    
    t.join();
    
    return 0;
}

A std::future can only be get()-ed once. After that, its shared state is empty. Trying to get the result again will throw an error. This is a crucial design point of std::future.

Problematic Code Example

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

int main() {
    std::future<int> f = std::async(std::launch::async, []() {
        return 100;
    });

    try {
        int result1 = f.get(); //  First get is okay
        std::cout << "First result: " << result1 << std::endl;
        
        int result2 = f.get(); //  Throws std::future_error: future_already_retrieved
        std::cout << "Second result: " << result2 << std::endl;
        
    } catch (const std::future_error& e) {
        std::cout << "Caught a future_error: " << e.what() << std::endl;
        if (e.code() == std::future_errc::future_already_retrieved) {
            std::cout << "Reason: You tried to get the result twice." << std::endl;
        }
    }
    
    return 0;
}

Solution
If you need to access the result of a future multiple times, use a std::shared_future. It's a non-movable future that allows multiple get() calls.

Corrected Code Example

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

int main() {
    std::future<int> f = std::async(std::launch::async, []() {
        return 100;
    });

    // Convert std::future to std::shared_future
    std::shared_future<int> sf = f.share();
    
    try {
        int result1 = sf.get(); //  First get is okay
        std::cout << "First result: " << result1 << std::endl;
        
        int result2 = sf.get(); //  Second get is also okay now!
        std::cout << "Second result: " << result2 << std::endl;
        
    } catch (const std::future_error& e) {
        std::cout << "Caught a future_error: " << e.what() << std::endl;
    }
    
    return 0;
}

std::shared_future is great for scenarios where multiple threads need to wait for and access the same asynchronous result.


cpp



Why Not Use std::valarray? Exploring Alternatives for Numerical Swapping in C++

The non-member function std::swap for std::valarray is a specialized version of the standard std::swap algorithm. Its purpose is simple to efficiently exchange the contents of two std::valarray objects of the same type and size


Exploring C++'s is_character_file and Alternatives

First off, what does is_character_file do? It checks if a given directory entry points to a character special file. These files represent devices that handle data one character at a time


Mastering Time in C++: A Practical Look at the Library

The std::chrono::choose utility doesn't exist in the C++ standard library. It's likely a misunderstanding or a typo. The C++ standard library's <chrono> header provides a set of powerful tools for handling time durations



Avoiding std::bad_variant_access: The Role of std::variant_npos

In C++'s std::variant, std::variant_npos is a special constant that represents an invalid index. It's defined as the largest value representable by std::size_t


Common Issues and Alternatives for std::chrono::year_month in C++

First, let's quickly review what std::chrono::year_month is. It's a part of the C++ <chrono> library introduced in C++20


Common Pitfalls of std::set::erase and How to Avoid Them

The std::set::erase method is great, but it can cause some tricky situations, especially when you're erasing elements while iterating through a set


C++ String Comparison: Why std::strcoll is Tricky and What to Use Instead

The most frequent problem with std::strcoll is not setting the locale correctly. If you don't call std::setlocale first


C++ Error Handling: The Legacy of std::unexpected

If you're working with older code that uses std::unexpected and you need to update it, the modern approach is to use exceptions for error handling


Avoiding Abstract Class Errors: The Power of C++'s std::is_abstract

Here's a friendly, detailed breakdown covering what it is, common issues, and alternatives with code examples.std::is_abstract<T> is a type trait introduced in C++11


Understanding operator() for Piecewise Linear Distributions in C++ Numerics

The std::piecewise_linear_distribution is a C++ Standard Library random number distribution that produces floating-point values (e.g., double) according to a piecewise linear probability density function (PDF)