Namespaces
Variants
Actions

Talk:cpp/thread/promise/set value at thread exit

From cppreference.com
< Talk:cpp‎ | thread‎ | promise

[edit] Example code

Is the example code with the lambda capturing the promise (i.e. creating a thread-local promise) valid?

Because the standard says:

30.6.5.21 "set_value_at_thread_exit ... Schedules that state to be made ready when the current thread exits, after all objects of thread storage duration associated with the current thread have been destroyed".

That is including the captured promise.

30.6.5.7 "~promise(); Effects: Abandons any shared state (30.6.4)."

30.6.4.7 "When an asynchronous provider is said to abandon its shared state, it means: - first, if that state is not ready, the provider stores an exception object of type future_error with an error condition of broken_promise within its shared state;..."

Maybe it would be better to capture the promise by reference?

it is valid, and demonstrates the intended use of promises. Nothing in this example has thread storage duration. --Cubbi (talk) 08:57, 24 April 2016 (PDT)
It doesn't have thread storage duration, but it also doesn't survive thread exit. A literal reading of the specification of std::thread's constructor suggests that the lambda's (and so the promise's) destructor behaves as if it executes immediately after the INVOKE call, presumably before thread storage duration objects are destroyed. It's a more subtle version of LWG2532. T. Canens (talk) 13:25, 24 April 2016 (PDT)
Okay, I'll be fine with this example redone to have a promise that outlives its thread pending resolution of 2532. --Cubbi (talk) 15:16, 24 April 2016 (PDT)