std::shared_future::wait_for
From cppreference.com
< cpp | thread | shared future
template< class Rep, class Period >
std::future_status wait_for( const std::chrono::duration<Rep,Period>& timeout_duration ) const; |
||
Waits for the result to become available. Blocks until specified timeout_duration
has elapsed or the result becomes available, whichever comes first. Returns value identifies the state of the result.
A steady clock is used to measure the duration. This function may block for longer than timeout_duration
due to scheduling or resource contention delays.
The behavior is undefined if valid() == false before the call to this function.
Contents |
[edit] Parameters
timeout_duration | - | maximum duration to block for |
[edit] Return value
Constant | Explanation |
future_status::deferred | The function to calculate the result has not been started yet |
future_status::ready | The result is ready |
future_status::timeout | The timeout has expired |
[edit] Exceptions
(none)
[edit] Notes
The implementations are encouraged to detect the case when valid == false before the call and throw a future_error with an error condition of future_errc::no_state.
[edit] Example
#include <iostream> #include <future> #include <thread> #include <chrono> int main() { std::shared_future<int> future = std::async(std::launch::async, [](){ std::this_thread::sleep_for(std::chrono::seconds(3)); return 8; }); std::cout << "waiting...\n"; std::future_status status; do { status = future.wait_for(std::chrono::seconds(1)); if (status == std::future_status::deferred) { std::cout << "deferred\n"; } else if (status == std::future_status::timeout) { std::cout << "timeout\n"; } else if (status == std::future_status::ready) { std::cout << "ready!\n"; } } while (status != std::future_status::ready); std::cout << "result is " << future.get() << '\n'; }
Possible output:
waiting... timeout timeout ready! result is 8
[edit] See also
waits for the result to become available (public member function) |
|
waits for the result, returns if it is not available until specified time point has been reached (public member function) |