Main Page | Namespace List | Class List | File List | Namespace Members | Class Members | File Members

Dv::Thread::Monitor Class Reference

A simple Monitor class using posix threads. More...

#include <monitor.h>

List of all members.

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.
Monitoroperator= (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.


Detailed Description

A simple Monitor class using posix threads.

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

See also:
Dv::Thread::Thread


Constructor & Destructor Documentation

Dv::Thread::Monitor::Monitor const std::string &  name,
unsigned int  n_conditions = 0,
std::ostream *  dbg = 0
 

Constructor.

Parameters:
name of this monitor.
n_conditions number of conditions in this monitor.
dbg if non-zero, debug output will be written to *dbg.

virtual Dv::Thread::Monitor::~Monitor  )  [virtual]
 

Virtual destructor.

Dv::Thread::Monitor::Monitor const Monitor  )  [private]
 

No copy ctor.


Member Function Documentation

void Dv::Thread::Monitor::enter  )  throw (std::runtime_error)
 

Enter the monitor.

Exceptions:
runtime_error if this thread already owns the monitor

void Dv::Thread::Monitor::exit  )  throw (std::runtime_error)
 

Exit the monitor.

Exceptions:
runtime_error if this thread is not the current owner of the monitor

void Dv::Thread::Monitor::wait unsigned int  condition = 0  )  throw (std::runtime_error)
 

Unconditionally wait for a given condition.

Parameters:
condition number of the condition to wait for.
Exceptions:
runtime_error if this thread is not the current owner of the monitor

bool Dv::Thread::Monitor::wait unsigned int  condition,
time_t  timeout
throw (std::runtime_error)
 

Wait for a given condition or until timeout millisecs elapsed.

Parameters:
condition number of the condition to wait for.
timeout number of milliseconds we are prepared to wait.
Returns:
false if a timeout occured.
Exceptions:
runtime_error if this thread is not the current owner of the monitor

void Dv::Thread::Monitor::signal unsigned int  condition = 0  )  const throw (std::runtime_error)
 

Signal a given condition.

This function unblocks at least one thread blocked on the condition. The unblocked threads contend for the monitor.

Parameters:
condition to signal.
Exceptions:
runtime_error if this thread is not the current owner of the monitor

void Dv::Thread::Monitor::broadcast unsigned int  condition = 0  )  const throw (std::runtime_error)
 

Signal all threads waiting on this condition.

This function unblocks all the threads blocked on the condition. The unblocked threads contend for the monitor.

Parameters:
condition to signal.
Exceptions:
runtime_error if this thread is not the current owner of the monitor

const std::string& Dv::Thread::Monitor::name  )  const [inline]
 

Returns:
name of this monitor.

void Dv::Thread::Monitor::check const std::string &  message  )  const throw (std::runtime_error) [private]
 

This functions throws a runtime_error if the current thread is not the current owner of the monitor.

Parameters:
message used as part of runtime_error.what()
Exceptions:
runtime_error if this thread is not the current owner of the monitor

void Dv::Thread::Monitor::check_cond const std::string &  msg,
unsigned int  n
const throw (std::runtime_error) [private]
 

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.

Parameters:
msg used as part of runtime_error.what()
n number of condition to check
Exceptions:
runtime_error if this thread is not the current owner of the monitor

Monitor& Dv::Thread::Monitor::operator= const Monitor  )  [private]
 

No assignment operator.


Member Data Documentation

std::string Dv::Thread::Monitor::name_ [private]
 

Name of the monitor.

pthread_t Dv::Thread::Monitor::owner_ [private]
 

Thread that currently owns the monitor.

pthread_mutex_t Dv::Thread::Monitor::mutex_ [private]
 

Entry to the monitor is controlled by this mutex semaphore.

pthread_mutexattr_t Dv::Thread::Monitor::mutex_attributes_ [private]
 

Attributes of mutex_.

unsigned int Dv::Thread::Monitor::n_conditions_ [private]
 

Number of conditions of the monitor.

pthread_cond_t* Dv::Thread::Monitor::conditions_ [private]
 

Array of n_conditions_ posix conditions of this monitor.

pthread_condattr_t Dv::Thread::Monitor::condition_attributes_ [private]
 

std::ostream* Dv::Thread::Monitor::dbg_ [private]
 

Write debug output to stream if not 0.


The documentation for this class was generated from the following file:
dvthread-0.5.0 [22 June, 2006]