std::condition_variable::wait_for
From cppreference.com
< cpp | thread | condition variable
template< class Rep, class Period > std::cv_status wait_for( std::unique_lock<std::mutex>& lock, |
(1) | (since C++11) |
template< class Rep, class Period, class Predicate > bool wait_for( std::unique_lock<std::mutex>& lock, |
(2) | (since C++11) |
wait_for
causes the current thread to block until the condition variable is notified, the given duration has been elapsed, or a spurious wakeup occurs. pred can be optionally provided to detect spurious wakeup.
1) Equivalent to return wait_until(lock, std::chrono::steady_clock::now() + rel_time);.
2) Equivalent to return wait_until(lock, std::chrono::steady_clock::now() + rel_time, std::move(pred));.
This overload may be used to ignore spurious awakenings while waiting for a specific condition to become true.
Right after wait_for
returns, lock.owns_lock() is true, and lock.mutex() is locked by the calling thread. If these postconditions cannot be satisfied[1], calls std::terminate.
If any of the following conditions is satisfied, the behavior is undefined:
- lock.owns_lock() is false.
- lock.mutex() is not locked by the calling thread.
- If some other threads are also waiting on *this, lock.mutex() is different from the mutex unlocked by the waiting functions (wait,
wait_for
and wait_until) called on *this by those threads.
- ↑ This can happen if the re-locking of the mutex throws an exception.
Contents |