std::promise<R>::set_value_at_thread_exit

Stores the value into the shared state without making the state ready immediately. The state is made ready when the current thread exits, after all variables with thread-local storage duration have been destroyed.

# Declarations

Main template
void set_value_at_thread_exit( const R& value );

(since C++11)

void set_value_at_thread_exit( R&& value );

(since C++11)

std::promise<R&> specializations
void set_value_at_thread_exit( R& value );

(since C++11)

std::promise<void> specialization
void set_value_at_thread_exit();

(since C++11)

# Parameters

# Return value

(none)

# Example

#include <future>
#include <iostream>
#include <thread>
 
int main()
{
    using namespace std::chrono_literals;
    std::promise<int> p;
    std::future<int> f = p.get_future();
    std::thread([&p]
    {
        std::this_thread::sleep_for(1s);
        p.set_value_at_thread_exit(9);
    }).detach();
 
    std::cout << "Waiting... " << std::flush;
    f.wait();
    std::cout << "Done!\nResult is: " << f.get() << '\n';
}

# Defect reports

DRApplied toBehavior as publishedCorrect behavior
LWG 2098C++11it was unclear which exceptions are required to be thrownmade clear

# See also