condition_variable_any::wait_for Method

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

template<
   class Lock,
   class Rep,
   class Period
>
bool wait_for(
   Lock& Lck,
   const chrono::duration<Rep,
   Period>& Rel_time
);
template<
   class Lock,
   class Rep,
   class Period,
   class Predicate
>
bool wait_for(
   Lock& Lck,
   const chrono::duration<Rep,
   Period>& Rel_time,
   Predicate Pred
);

Parameters

  • Lck
    A mutex object of any type.

  • 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_any 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.

The second method in effect 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_any Class

<condition_variable>