Share via


condition_variable::wait_for Method

Blocks a thread, and sets a time interval after which the thread unblocks.

template<
   class Rep,
   class Period
>
cv_status wait_for(
   unique_lock<mutex>& Lck,
   const chrono::duration<Rep,
   Period>& Rel_time
);
template<
   class Rep,
   class Period,
   class Predicate
>
bool wait_for(
   unique_lock<mutex>& Lck,
   const chrono::duration<Rep,
   Period>& Rel_time,
   PredicatePred
);

Parameters

  • Lck
    A unique_lock<mutex> object.

  • Rel_time
    A chrono::duration object that specifies the amount of time before the thread wakes up.

  • Pred
    Any expression that returns true or false.

Return Value

The first method returns cv_status::timeout if the wait terminates when Rel_time has elapsed. Otherwise, the method returns cv_status::no_timeout.

The second method returns the value of Pred.

Remarks

The first method blocks until the condition_variable object is signaled by a call to notify_one or notify_all or until the time interval Rel_time has elapsed. It can also wake up spuriously.

In effect, the second method executes the following code.

while(!Pred())
   if(wait_for(Lck, Rel_time) == cv_status::timeout)
      return Pred();
return true;

Requirements

Header: condition_variable

Namespace: std

See Also

Reference

condition_variable Class

<condition_variable>