Is it possible to check if a std::future
has finished or not? As far as I can tell the only way to do it would be to call wait_for
with a zero duration and check if the status is ready
or not, but is there a better way?
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Difference between Thread#run and Thread#wakeup?
- Selecting only the first few characters in a strin
- Java/Spring MVC: provide request context to child
- What exactly do pointers store? (C++)
You are correct, and apart from calling
wait_until
with a time in the past (which is equivalent) there is no better way.You could always write a little wrapper if you want a more convenient syntax:
N.B. if the function is deferred this will never return true, so it's probably better to check
wait_for
directly in the case where you might want to run the deferred task synchronously after a certain time has passed or when system load is low.My first bet would be to call
wait_for
with a 0 duration, and check the result code that can be one offuture_status::ready
,future_status::deferred
orfuture_status::timeout
.In cppreference they claim that
valid()
checks if the result is available, but the standard says thatvalid()
will returntrue
if*this
refers to a shared state, independently of whether that state is ready or not.There's an is_ready member function in the works for std::future. In the meantime, the VC implementation has an _Is_ready() member.