nidas v1.2.3
Public Member Functions | Private Member Functions | Private Attributes | List of all members
nidas::util::Cond Class Reference

A wrapper class for a Posix condition variable. More...

#include <ThreadSupport.h>

Public Member Functions

 Cond () throw ()
 Construct a POSIX condition variable, with default attributes.
 
 Cond (const Cond &x) throw ()
 Copy constructor.
 
 ~Cond ()
 Destruct a Cond.
 
void lock ()
 Lock the mutex associated with the condition variable.
 
void unlock ()
 Unlock the mutex associated with the condition variable.
 
void signal ()
 Unblock at least one thread waiting on the condition variable.
 
void broadcast ()
 Restart all threads waiting on the condition variable.
 
void wait ()
 Wait on the condition variable.
 

Private Member Functions

Condoperator= (const Cond &)
 No assignment allowed.
 

Private Attributes

pthread_cond_t _p_cond
 
Mutex _mutex
 

Detailed Description

A wrapper class for a Posix condition variable.

Constructor & Destructor Documentation

◆ Cond() [1/2]

Cond::Cond ( )
throw ( )

Construct a POSIX condition variable, with default attributes.

See man page for pthread_cond_init.

References _p_cond.

◆ Cond() [2/2]

Cond::Cond ( const Cond & x)
throw ( )

Copy constructor.

Creates new unlocked condition variable and mutex.

◆ ~Cond()

Cond::~Cond ( )

Destruct a Cond.

See man page for pthread_cond_destroy. Cond must not be waited on by any thread, otherwise std::terminate() is called.

References _p_cond, and CLOG.

Member Function Documentation

◆ broadcast()

void nidas::util::Cond::broadcast ( )
inline

Restart all threads waiting on the condition variable.

See also
lock().

References _p_cond.

Referenced by PacketReader::loop(), nidas::core::SampleBuffer::run(), nidas::core::SampleSorter::run(), nidas::util::Multisync::sync(), and nidas::util::Multisync::sync().

◆ lock()

void nidas::util::Cond::lock ( )
inline

Lock the mutex associated with the condition variable.

Here is an example (lifted from the pthread_cond_init man page): of two threads sharing variables x and y. One thread waits for a signal that x is greater than y:

int x,y;
Cond xyCond;
...
xyCond.lock();
while (x <= y) xyCond.wait();
// operate on x and y
xyCond.unlock();
A wrapper class for a Posix condition variable.
Definition ThreadSupport.h:258
void lock()
Lock the mutex associated with the condition variable.
Definition ThreadSupport.h:304

Another thread manipulates x and y and signals when x > y:

xyCond.lock();
// modify x and y
if (x > y) xyCond.signal();
xyCond.unlock();
Exceptions
Exception

References _mutex, and nidas::util::Mutex::lock().

Referenced by nidas::core::SampleOutputRequestThread::addConnectRequest(), nidas::core::SampleOutputRequestThread::addDeleteRequest(), nidas::util::Autolock::Autolock(), nidas::core::SampleOutputRequestThread::clear(), nidas::dynld::raf::SyncRecordReader::endOfStream(), nidas::core::SampleSorter::flush(), nidas::core::SampleBuffer::heapDecrement(), nidas::core::SampleSorter::heapDecrement(), nidas::core::SampleBuffer::interrupt(), nidas::core::SampleOutputRequestThread::interrupt(), nidas::core::SampleSorter::interrupt(), nidas::core::SensorOpener::interrupt(), PacketReader::loop(), nidas::dynld::raf::SyncRecordReader::nextSample(), nidas::core::SensorOpener::openSensor(), nidas::core::SensorOpener::reopenSensor(), WriterThread::run(), nidas::core::SampleBuffer::run(), nidas::core::SampleOutputRequestThread::run(), nidas::core::SampleSorter::run(), nidas::core::SensorOpener::run(), nidas::util::Synchronized::Synchronized(), PacketReader::~PacketReader(), and nidas::dynld::raf::SyncRecordReader::~SyncRecordReader().

◆ operator=()

Cond & nidas::util::Cond::operator= ( const Cond & )
private

No assignment allowed.

◆ signal()

void nidas::util::Cond::signal ( )
inline

◆ unlock()

void nidas::util::Cond::unlock ( )
inline

◆ wait()

void Cond::wait ( )

Wait on the condition variable.

See also
lock() for an example. wait() does several things:
  1. It immediately unlocks the mutex
  2. It blocks until the condition variable is signalled
  3. It locks the mutex again wait() is a cancellation point, which sets up the appropriate cancellation cleanup handlers so that the mutex is unlocked if the thread is cancelled.
Exceptions
Exception

References _mutex, _p_cond, and nidas::util::Mutex::ptr().

Referenced by nidas::core::SampleSorter::flush(), nidas::dynld::raf::SyncRecordReader::nextSample(), WriterThread::run(), nidas::core::SampleBuffer::run(), nidas::core::SampleOutputRequestThread::run(), nidas::core::SampleSorter::run(), nidas::core::SensorOpener::run(), nidas::util::Multisync::sync(), and nidas::util::Multisync::sync().

Member Data Documentation

◆ _mutex

Mutex nidas::util::Cond::_mutex
private

Referenced by lock(), unlock(), and wait().

◆ _p_cond

pthread_cond_t nidas::util::Cond::_p_cond
private

Referenced by broadcast(), Cond(), signal(), wait(), and ~Cond().


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