Module condition_variable

Module condition_variable 

Source
Expand description

§Condition Variable.

A Condition Variable allows a thread to efficiently block until a certain condition is met, without consuming CPU cycles. It is always used in conjunction with a Mutex that guards access to shared data. It is generally used when a thread needs to wait for a specific state in shared data, and another thread will notify it when that state changes.

Condition variables enable complex synchronization patterns such as thread coordination, resource availability signaling, or implementing blocking queues.

§ConditionVariable in KeOS

Condition variable must work with the shared Mutex. To enforce this, KeOS’s ConditionVariable api takes either Mutex or MutexGuard as an argument. This enforces that the apis are called with a mutex, but does not fully ensure that the mutex is the associated one.

The ConditionVariable::wait_while method automatically checks the predicate, blocks the current thread if the condition is true, and re-checks it upon wakeup. The method takes care of locking, checking the condition, blocking, and waking up:

let guard = condvar.wait_while(&mutex, |state| state.is_empty());

There are two signaling methods that takes the MutexGuard:

§Implementation Requirements

You need to implement the followings:

After implement the functionalities, move on to the next section.

Structs§

ConditionVariable
A Condition Variable