Who owns the shared state in futures and promises? In particular, who is responsible for creating and deleting a common state in these classes? Or is this a general condition that should be considered a link? I cannot get an answer by reading the docs for them at cppreference.
The way I thought about this was the easiest way to make the class std::promise
responsible for creating a shared state, but then pass it to std::future
, which is retrieved from std::promise
for deletion when the future is destroyed. But then this approach can lead to tattered items of promise. Therefore, I’m not sure how the state should actually be divided between them.
For example, the code below creates undefined behavior (since the shared state can be destroyed when the future is destroyed)?
auto prom = std::promise<void>{};
{
auto fut = prom.get_future();
}
prom.set_value();
In addition, the docs std::promise::~promise
on cppreference say "if the general state is ready, release it", which makes me think that the general state is not counted by reference.
source
share