Mistake on this page?
Report an issue in GitHub or email us
Data Structures | Public Member Functions | Static Protected Member Functions | Protected Attributes
rtos::ConditionVariable Class Reference

#include <ConditionVariable.h>

Inheritance diagram for rtos::ConditionVariable:
mbed::NonCopyable< ConditionVariable > TestConditionVariable

Data Structures

struct  Waiter
 

Public Member Functions

 ConditionVariable (Mutex &mutex)
 
void wait ()
 
bool wait_until (uint64_t millisec)
 
bool wait_for (uint32_t millisec)
 
void notify_one ()
 
void notify_all ()
 
 ~ConditionVariable ()
 

Static Protected Member Functions

static void _add_wait_list (Waiter **wait_list, Waiter *waiter)
 
static void _remove_wait_list (Waiter **wait_list, Waiter *waiter)
 

Protected Attributes

Mutex_mutex
 
Waiter_wait_list
 

Detailed Description

This class provides a safe way to wait for or send notifications of condition changes

This class is used in conjunction with a mutex to safely wait for or notify waiters of condition changes to a resource accessible by multiple threads.

Defined behavior

Undefined behavior

Note
Synchronization level: Thread safe

Example:

#include "mbed.h"
Mutex mutex;
ConditionVariable cond(mutex);
// These variables are protected by locking mutex
uint32_t count = 0;
bool done = false;
void worker_thread()
{
mutex.lock();
do {
printf("Worker: Count %lu\r\n", count);
// 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 notify waiters of this
count++;
printf("Main: Set count to %lu\r\n", count);
cond.notify_all();
mutex.unlock();
wait(1.0);
}
mutex.lock();
// Change done and notify waiters of this
done = true;
printf("Main: Set done\r\n");
cond.notify_all();
mutex.unlock();
thread.join();
}

The documentation for this class was generated from the following files:
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.