5

Note that this is different from calling valid(). valid() indicates whether the future has a valid shared state at all, however what I want to know is whether a value for the future has been set (i.e. via std::promise::set_value).

There's a wait_for method, however I don't want the calling thread to block at all, I just want to check for the presence of a value. Theoretically, I could call wait_for with a zero duration, but I don't know if it's actually the preferred way of doing this and I'm not sure what the expected behavior is when wait_for is called with a zero duration.

nicebyte
  • 1,498
  • 11
  • 21

1 Answers1

0

The aim of using std::future, is to be able to run a task asynchronously. Thus only call call get() on the future when you need the value, because the thread blocks until the future is ready and then returns the value.

ivanw
  • 491
  • 8
  • 16
  • I don't think thats always true, what about when you are in an "eventual consistency" scenario? You might not always want to block on a future. – Curious Jul 07 '16 at 17:18
  • you can wait on a future with a timeout – yano Jun 05 '17 at 17:19