nidas  v1.2-1520
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. More...
 
 Cond (const Cond &x) throw ()
 Copy constructor. More...
 
 ~Cond ()
 Destruct a Cond. More...
 
void lock () throw (Exception)
 Lock the mutex associated with the condition variable. More...
 
void unlock () throw (Exception)
 Unlock the mutex associated with the condition variable. More...
 
void signal () throw (Exception)
 Unblock at least one thread waiting on the condition variable. More...
 
void broadcast ()
 Restart all threads waiting on the condition variable. More...
 
void wait () throw (Exception)
 Wait on the condition variable. More...
 

Private Member Functions

Condoperator= (const Cond &)
 No assignment allowed. More...
 

Private Attributes

pthread_cond_t _p_cond
 
Mutex _mutex
 

Detailed Description

A wrapper class for a Posix condition variable.

Constructor & Destructor Documentation

Cond::Cond ( )
throw (
)

Construct a POSIX condition variable, with default attributes.

See man page for pthread_cond_init.

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

Copy constructor.

Creates new unlocked condition variable and mutex.

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, CLOG, and nidas::util::Exception::what().

Member Function Documentation

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(), and nidas::util::Multisync::sync().

void nidas::util::Cond::lock ( )
throw (Exception
)
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();

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

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

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

Referenced by nidas::util::McSocket< SocketT >::accept(), nidas::core::SampleOutputRequestThread::addConnectRequest(), nidas::core::SampleOutputRequestThread::addDeleteRequest(), nidas::core::SampleOutputRequestThread::clear(), nidas::util::McSocket< SocketT >::connect(), nidas::dynld::raf::SyncRecordReader::endOfStream(), nidas::core::SampleSorter::flush(), nidas::core::SampleBuffer::heapDecrement(), nidas::core::SampleSorter::heapDecrement(), nidas::core::SensorOpener::interrupt(), nidas::core::SampleBuffer::interrupt(), nidas::core::SampleSorter::interrupt(), PacketReader::loop(), nidas::dynld::raf::SyncRecordReader::nextSample(), nidas::util::McSocket< SocketT >::offer(), nidas::core::SensorOpener::openSensor(), nidas::core::SensorOpener::reopenSensor(), nidas::core::SampleOutputRequestThread::run(), nidas::core::SensorOpener::run(), nidas::core::SampleBuffer::run(), nidas::core::SampleSorter::run(), WriterThread::run(), PacketReader::~PacketReader(), and nidas::dynld::raf::SyncRecordReader::~SyncRecordReader().

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

No assignment allowed.

void nidas::util::Cond::signal ( )
throw (Exception
)
inline
void nidas::util::Cond::unlock ( )
throw (Exception
)
inline
void Cond::wait ( )
throw (Exception
)

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.

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

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

Member Data Documentation

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

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

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

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


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