nlib
nn::nlib::threading::CondVar Class Referencefinal

Conditional variable for synchronization. More...

#include "nn/nlib/threading/CondVar.h"

Public Member Functions

constexpr CondVar () noexcept=default
 Instantiates the object with default parameters (default constructor).
 
void Notify () noexcept
 Signals to at least one waiting thread. More...
 
void NotifyAll () noexcept
 Signals all waiting threads.
 
template<class lock_type >
errno_t Wait (lock_type &lock) noexcept NLIB_REQUIRES(lock)
 Waits. More...
 
template<class lock_type >
NLIB_CHECK_RESULT errno_t WaitFor (lock_type &lock, const nlib_ns::TimeSpan &timeout) noexcept NLIB_REQUIRES(lock)
 Waits with a specified timeout. More...
 
template<class lock_type >
NLIB_CHECK_RESULT errno_t WaitUntil (lock_type &lock, const nlib_ns::DateTime &datetime) noexcept NLIB_REQUIRES(lock)
 Waits with a timeout specified in date/time. More...
 

Detailed Description

Conditional variable for synchronization.

Description
Used to wake a thread when the shared data meets some condition.
Waits with the Wait function until the conditions are met. When the conditions are met, threads are awakened by Notify and NotifyAll.
The functionality is the same as CONDITION_VARIABLE in Windows or pthread_cond.
SimpleCriticalSection m;
CondVar cond;
bool flag;
if (!cond.Initialize()) { error; }
....
// Wait loop for common conditional variables.
m.lock();
while (!flag) {
// Waits for cond.Notify and cond.NotifyAll in a different thread.
// (The flag is made true at the same time.)
errno_t e = cond.Wait(m);
if (e != 0) { ERROR; }
// The flag is not guaranteed to be true when the thread is awakened.
// The flag value must be confirmed after this.
}
// Conduct an exclusive process.
....
flag = false;
m.unlock();

Definition at line 33 of file CondVar.h.

Member Function Documentation

§ Notify()

nn::nlib::threading::CondVar::Notify ( )
inlinenoexcept

Signals to at least one waiting thread.

Description
We recommend using NotifyAll, as some threads may not be able to receive a signal for a extended period of time.

Definition at line 49 of file CondVar.h.

§ Wait()

template<class lock_type >
nn::nlib::threading::CondVar::Wait ( lock_type &  lock)
inlinenoexcept

Waits.

Parameters
[in,out]lockClass like CriticalSection that includes the lock and unlock member functions.
Return values
0An error has not occurred.
ENOMEMIndicates that there are not enough system resources.

Definition at line 63 of file CondVar.h.

§ WaitFor()

template<class lock_type >
nn::nlib::threading::CondVar::WaitFor ( lock_type &  lock,
const nlib_ns::TimeSpan timeout 
)
inlinenoexcept

Waits with a specified timeout.

Parameters
[in,out]lockClass like CriticalSection that includes the lock and unlock member functions.
[in]timeoutSpecifies the timeout.
Return values
0An error has not occurred.
ETIMEDOUTIndicates a timeout.
ENOMEMIndicates that there are not enough system resources.

Definition at line 71 of file CondVar.h.

§ WaitUntil()

template<class lock_type >
nn::nlib::threading::CondVar::WaitUntil ( lock_type &  lock,
const nlib_ns::DateTime datetime 
)
inlinenoexcept

Waits with a timeout specified in date/time.

Parameters
[in,out]lockClass like CriticalSection that includes the lock and unlock member functions.
[in]datetimeTimeout limit.
Return values
0An error has not occurred.
ETIMEDOUTIndicates a timeout.
ENOMEMIndicates that there are not enough system resources.

Definition at line 76 of file CondVar.h.


The documentation for this class was generated from the following files: