mercury0114
mercury0114

Reputation: 1449

What happens if future.get() is called after promise.set_value()?

I create a promise and future objects:

Promise<int> p = promise<int>(); 
Future<int> f = p.get_future();

I pass the promise to the background thread, which eventually calls p.set_value(myInt).

In the main thread, I call f.get().

What if f.get() is called after p.set_value(myInt) is called?

Would f.get() immediately return myInt?

Upvotes: 3

Views: 497

Answers (1)

darune
darune

Reputation: 10972

According to the documentation it calls wait - in order to wait for a result. According to that description in turn, it is stated that:

Blocks until the result becomes available.

That seems to imply it should not block (even for a short while) if there is a result already. So except for some sanity checks, mutex handling or the like I would assume it to return immediately as per the description.

Do you have an actual issue or are you merely asking ?

Upvotes: 3

Related Questions