Who is responsible for the overall status of futures and promises

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::promiseresponsible for creating a shared state, but then pass it to std::future, which is retrieved from std::promisefor 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::~promiseon cppreference say "if the general state is ready, release it", which makes me think that the general state is not counted by reference.

+6
source share
1 answer

When an object std::future(or std::promise) is destroyed, it frees the general state.

This rule states that when an asynchronous returned object or asynchronous provider releases its general state, it refuses to reference the general state.

, .

, , , UB, prom .

+7

Source: https://habr.com/ru/post/1016516/


All Articles