#include <monitor.h>
Public Member Functions | |
Monitor (const std::string &name, unsigned int n_conditions=0, std::ostream *dbg=0) | |
Constructor. | |
virtual | ~Monitor () |
Virtual destructor. | |
void | enter () throw (std::runtime_error) |
Enter the monitor. | |
void | exit () throw (std::runtime_error) |
Exit the monitor. | |
void | wait (unsigned int condition=0) throw (std::runtime_error) |
Unconditionally wait for a given condition. | |
bool | wait (unsigned int condition, time_t timeout) throw (std::runtime_error) |
Wait for a given condition or until timeout millisecs elapsed. | |
void | signal (unsigned int condition=0) const throw (std::runtime_error) |
Signal a given condition. | |
void | broadcast (unsigned int condition=0) const throw (std::runtime_error) |
Signal all threads waiting on this condition. | |
const std::string & | name () const |
Private Member Functions | |
void | check (const std::string &message) const throw (std::runtime_error) |
This functions throws a runtime_error if the current thread is not the current owner of the monitor. | |
void | check_cond (const std::string &msg, unsigned int n) const throw (std::runtime_error) |
This functions throws a runtime_error if n is not a valid condition of this monitor or if the current thread does not own the monitor. | |
Monitor (const Monitor &) | |
No copy ctor. | |
Monitor & | operator= (const Monitor &) |
No assignment operator. | |
Private Attributes | |
std::string | name_ |
Name of the monitor. | |
pthread_t | owner_ |
Thread that currently owns the monitor. | |
pthread_mutex_t | mutex_ |
Entry to the monitor is controlled by this mutex semaphore. | |
pthread_mutexattr_t | mutex_attributes_ |
Attributes of mutex_. | |
unsigned int | n_conditions_ |
Number of conditions of the monitor. | |
pthread_cond_t * | conditions_ |
Array of n_conditions_ posix conditions of this monitor. | |
pthread_condattr_t | condition_attributes_ |
std::ostream * | dbg_ |
Write debug output to stream if not 0. |
The following defines a Buffer monitor with protected get() and put() access.
// $Id: buffer.h,v 1.6 2006/06/24 09:39:28 dvermeir Exp $ #ifndef DV_THREAD_BUFFER_H #define DV_THREAD_BUFFER_H #include <dvthread/lock.h> #define TRACE std::cerr << pthread_self() << ":" << __FILE__ << "." << __LINE__ << std::endl; // This class represents a buffer with synchronized access. class Buffer: public Dv::Thread::Monitor { public: Buffer() throw (std::runtime_error): Dv::Thread::Monitor("buffer",2), // &std::cerr), n_items_(0) {} // Add an item to a buffer. void put(int i) throw (std::runtime_error) { Dv::Thread::Lock lock(*this, "put", 0); // &std::cerr); // Get exclusive access to *this. while (n_items_==MAX) if (!wait(OK_TO_PUT,2000)) // Wait at most 2 secs. throw std::runtime_error("Buffer::put() timed out"); std::cerr << "put " << i << " "; data_[n_items_++] = i; // Actually put the item. signal(OK_TO_GET); } // Obtain and remove the last item in the buffer. int get() throw (std::runtime_error) { Dv::Thread::Lock lock(*this, "get"); // , &std::cerr); // Get exclusive access to *this. while (n_items_ == 0) { if (!wait(OK_TO_GET, 2000)) throw std::runtime_error("Buffer::get() timed out"); } int tmp = data_[--n_items_]; std::cerr << "get " << tmp << " "; signal(OK_TO_PUT); return tmp; } private: // Buffer capacity. enum { MAX = 3 }; // Names for conditions. enum { OK_TO_GET = 0, OK_TO_PUT = 1 }; /// conditions // Number of items in the buffer. int n_items_; // Actual store for items. int data_[MAX]; }; #endif
|
Constructor.
|
|
Virtual destructor.
|
|
No copy ctor.
|
|
Enter the monitor.
|
|
Exit the monitor.
|
|
Unconditionally wait for a given condition.
|
|
Wait for a given condition or until timeout millisecs elapsed.
|
|
Signal a given condition. This function unblocks at least one thread blocked on the condition. The unblocked threads contend for the monitor.
|
|
Signal all threads waiting on this condition. This function unblocks all the threads blocked on the condition. The unblocked threads contend for the monitor.
|
|
|
|
This functions throws a runtime_error if the current thread is not the current owner of the monitor.
|
|
This functions throws a runtime_error if n is not a valid condition of this monitor or if the current thread does not own the monitor.
|
|
No assignment operator.
|
|
Name of the monitor.
|
|
Thread that currently owns the monitor.
|
|
Entry to the monitor is controlled by this mutex semaphore.
|
|
Attributes of mutex_.
|
|
Number of conditions of the monitor.
|
|
Array of n_conditions_ posix conditions of this monitor.
|
|
|
|
Write debug output to stream if not 0.
|
dvthread-0.5.0 | [22 June, 2006] |