Wait until the std::condition_variable is woken
by a call to notify_one()
or notify_all(),
and the predicate is true,
or until the specified time period has elapsed.
template<typename Rep,typename Period, typename Predicate> bool wait_for(std::unique_lock<std::mutex>& lock, std::chrono::duration<Rep,Period> const& relative_time, Predicate pred);
- Preconditions:
The expression
pred()shall be valid, and shall return a value that is convertible tobool.lock.owns_lock()shall betrue, and the lock shall be owned by the thread callingwait().- Effects:
-
As-if
internal_clock::time_point end=internal_clock::now()+relative_time; while(!pred()) { std::chrono::duration<Rep,Period> remaining_time=end-internal_clock::now(); if(wait_for(lock,remaining_time)==std::cv_status::timeout) return pred(); } return true;
- Returns:
trueif the most recent call topred()returnedtrue,falseif the time period specified byrelative_timehas elapsed, andpred()returnedfalse.- Note:
The potential for spurious wake ups means that it is unspecified how many times
predwill be called.predwill always be invoked with the mutex referenced bylocklocked, and the function shall return if (and only if) an evaluation of(bool)pred()returnstrueor the time period specified byrelative_timehas elapsed. The thread may be blocked for longer than the specified duration. Where possible, the elapsed time is determined by a monotonic clock.- Throws:
Any exception thrown by a call to
pred, orstd::system_errorif the effects could not be achieved.- Synchronization:
Calls to
notify_one(),notify_all(),wait(),wait_for()andwait_until()on a singlestd::condition_variableinstance are serialized. A call tonotify_one()ornotify_all()will only wake threads that started waiting prior to that call.
Header
#include <condition_variable>