condition_variable Class

 

For the latest documentation on Visual Studio 2017 RC, see Visual Studio 2017 RC Documentation.

Use the condition_variable class to wait for an event when you have a mutex of type unique_lock<mutex>. Objects of this type may have better performance than objects of type condition_variable_any<unique_lock<mutex>>.

class condition_variable;

Public Constructors

NameDescription
condition_variable::condition_variable ConstructorConstructs a condition_variable object.

Public Methods

NameDescription
condition_variable::native_handle MethodReturns the implementation-specific type representing the condition_variable handle.
condition_variable::notify_all MethodUnblocks all threads that are waiting for the condition_variable object.
condition_variable::notify_one MethodUnblocks one of the threads that are waiting for the condition_variable object.
condition_variable::wait MethodBlocks a thread.
condition_variable::wait_for MethodBlocks a thread, and sets a time interval after which the thread unblocks.
condition_variable::wait_until MethodBlocks a thread, and sets a maximum point in time at which the thread unblocks.

Header: condition_variable

Namespace: std

Constructs a condition_variable object.

condition_variable();

Remarks

If not enough memory is available, the constructor throws a system_error object that has a not_enough_memory error code. If the object cannot be constructed because some other resource is not available, the constructor throws a system_error object that has a resource_unavailable_try_again error code.

Returns the implementation-specific type that represents the condition_variable handle.

native_handle_type native_handle();

Return Value

native_handle_type is defined as a pointer to Concurrency Runtime internal data structures.

Unblocks all threads that are waiting for the condition_variable object.

void notify_all() noexcept;

Unblocks one of the threads that are waiting on the condition_variable object.

void notify_one() noexcept;

Blocks a thread.

void wait(
    unique_lock<mutex>& Lck);

template <class Predicate>
void wait(
    unique_lock<mutex>& Lck,
    Predicate Pred);

Parameters

Lck
A unique_lock<mutex> object.

Pred
Any expression that returns true or false.

Remarks

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

In effect, the second method executes the following code.

while(!Pred())
    wait(Lck);

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, Predicate Pred);

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;

Blocks a thread, and sets a maximum point in time at which the thread unblocks.

template <class Clock, class Duration>
cv_status wait_until(
    unique_lock<mutex>& Lck,
    const chrono::time_point<Clock, Duration>& Abs_time);

template <class Clock, class Duration, class Predicate>
bool wait_until(
    unique_lock<mutex>& Lck,
    const chrono::time_point<Clock, Duration>& Abs_time, Predicate Pred);

cv_status wait_until(
    unique_lock<mutex>& Lck,
    const xtime* Abs_time);

template <class Predicate>
bool wait_until(
    unique_lock<mutex>& Lck,
    const xtime* Abs_time, Predicate Pred);

Parameters

Lck
A unique_lock<mutex> object.

Abs_time
A chrono::time_point object.

Pred
Any expression that returns true or false.

Return Value

Methods that return a cv_status type return cv_status::timeout if the wait terminates when Abs_time elapses. Otherwise, the methods return cv_status::no_timeout.

Methods that return a bool return 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 Abs_time. It can also wake up spuriously.

In effect, the second method executes the following code

while(!Pred())
    if(wait_until(Lck, Abs_time) == cv_status::timeout)
    return Pred();

return true;

The third and fourth methods use a pointer to an object of type xtime to replace the chrono::time_point object. The xtime object specifies the maximum amount of time to wait for a signal.

Header Files Reference
<condition_variable>

Show: