Who owns the shared state in futures and promises? In particular who is responsible for the construction and deletion of the shared state in these classes? Or is the shared state supposed to be reference counted? I am not able to get an answer by reading the docs for these on cppreference.
The way I was thinking about it the easiest thing to do would be to have the std::promise
class be responsible for the creation of the shared state, but then hand it off to the std::future
that is fetched from the std::promise
for deletion when the future is destroyed. But then this approach can lead to dangling promise objects. So I am not sure how really the state is supposed to be shared between the two.
For example does the code below produce undefined behavior (since the shared state might be destroyed when the future is destroyed)?
auto prom = std::promise<void>{};
{
auto fut = prom.get_future();
}
prom.set_value();
Further, the docs for std::promise::~promise
on cppreference say "if the shared state is ready, releases it" which gets me to think that the shared state is not reference counted.
shared_ptr<State>
would have. Which implies reference counting – Dissemblance