Mistake on this page?
Report an issue in GitHub or email us

ConditionVariable

ConditionVariable class hierarchy

The ConditionVariable class provides a mechanism to safely wait for or signal state changes. A common scenario when writing multithreaded code is to protect shared resources with a mutex and then release that mutex to wait for a change of that data. If you do not do this carefully, this can lead to a race condition in the code. A condition variable provides a safe solution to this problem by handling the wait for a state change, along with releasing and acquiring the mutex automatically during this waiting period. Note that you cannot make wait or notify calls on a condition variable from ISR context. Unlike EventFlags, ConditionVariable does not let you wait on multiple events at the same time.

ConditionVariable class reference

Public Member Functions
 ConditionVariable (Mutex &mutex)
 Create and initialize a ConditionVariable object. More...
void wait ()
 Wait for a notification. More...
template<typename Predicate >
void wait (Predicate pred)
 Wait for a predicate. More...
bool wait_until (uint64_t millisec)
 Wait for a notification until the specified time. More...
cv_status wait_until (Kernel::Clock::time_point abs_time)
 Wait for a notification until the specified time. More...
template<class Predicate >
bool wait_until (Kernel::Clock::time_point abs_time, Predicate pred)
 Wait for a predicate until the specified time. More...
bool wait_for (uint32_t millisec)
 Wait for a notification or timeout. More...
cv_status wait_for (Kernel::Clock::duration_u32 rel_time)
 Wait for a notification or timeout. More...
template<class Predicate >
bool wait_for (Kernel::Clock::duration rel_time, Predicate pred)
 Wait for a predicate or timeout. More...
void notify_one ()
 Notify one waiter on this condition variable that a condition changed. More...
void notify_all ()
 Notify all waiters on this condition variable that a condition changed. More...
 ~ConditionVariable ()
 ConditionVariable destructor. More...

ConditionVariable example

Below is an example of ConditionVariable usage, where one thread is generating events every 1s, and the second thread is waiting for the events and executing an action.

/*
 * Copyright (c) 2018-2020 Arm Limited and affiliates.
 * SPDX-License-Identifier: Apache-2.0
 */

#include "mbed.h"

Mutex mutex;
ConditionVariable cond(mutex);

// These variables are protected by locking mutex
uint32_t counter = 0;
bool done = false;

void worker_thread()
{
    mutex.lock();
    do {
        printf("Worker: Count %lu\r\n", counter);

        // Wait for a condition to change
        cond.wait();

    } while (!done);
    printf("Worker: Exiting\r\n");
    mutex.unlock();
}

int main()
{
    Thread thread;
    thread.start(worker_thread);

    for (int i = 0; i < 5; i++) {

        mutex.lock();
        // Change count and signal this
        counter++;
        printf("Main: Set count to %lu\r\n", counter);
        cond.notify_all();
        mutex.unlock();

        ThisThread::sleep_for(1000);
    }

    mutex.lock();
    // Change done and signal this
    done = true;
    printf("Main: Set done\r\n");
    cond.notify_all();
    mutex.unlock();

    thread.join();
}

Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.