Wait until the std::condition_variable_any 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 Lockable,typename Rep, typename Period, typename Predicate> bool wait_for( Lockable& 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.Lockablemeets theLockableRequirements, andlockowns a lock.- 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 steady 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_variable_anyinstance are serialized. A call tonotify_one()ornotify_all()will only wake threads that started waiting prior to that call.
Header
#include <condition_variable>