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

Dv::Thread::Lock Class Reference

Convenience class to synchronize access to a Monitor object. More...

#include <lock.h>

Collaboration diagram for Dv::Thread::Lock:

Collaboration graph
[legend]
List of all members.

Public Member Functions

 Lock (Monitor &m, const std::string &msg="", std::ostream *log=0)
 Constructor.
 ~Lock () throw (std::runtime_error)
 Destructor.

Private Attributes

Monitormonitor_
 Monitor on which lock is defined.
const std::string msg_
 Message to print on construction and destruction.
std::ostream * log_
 If non-zero, pointer to stream on which to log info.

Detailed Description

Convenience class to synchronize access to a Monitor object.

Creating a lock enters the monitor, destroying the lock exits the monitor.

Example usage:

 class SomeClassToProtect: public Dv::Thread::Monitor {
 public:
   voif f() {
     Lock  lock(*this, "f", &cerr); // enter monitor
     access the object
     // destructor of lock will exit the monitor.
     }
 ...
 };

The lock declaration can be abbreviated using the Java-inspired SYNCHRONIZED macro.


Constructor & Destructor Documentation

Dv::Thread::Lock::Lock Monitor m,
const std::string &  msg = "",
std::ostream *  log = 0
 

Constructor.

This function will enter the monitor parameter.

Parameters:
m monitor to enter.
msg optional message to print to log
log if nonzero, the message will be printed both on creation and destruction of the lock.
See also:
Monitor::enter

Dv::Thread::Lock::~Lock  )  throw (std::runtime_error)
 

Destructor.

This function will exit the monitor.

Exceptions:
runtime_error if the current thread is not in the monitor (e.g. if m.exit() was called before the destructor of the Lock).
See also:
Monitor::exit()


Member Data Documentation

Monitor& Dv::Thread::Lock::monitor_ [private]
 

Monitor on which lock is defined.

const std::string Dv::Thread::Lock::msg_ [private]
 

Message to print on construction and destruction.

std::ostream* Dv::Thread::Lock::log_ [private]
 

If non-zero, pointer to stream on which to log info.


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