Main Page   Namespace List   Class Hierarchy   Compound List   Compound Members  

ZThread::Guard Class Template Reference

#include <Guard.h>

List of all members.

Public Methods

 Guard (LOCK &l)
 Guard (LOCK &l, unsigned long timeout) throw (Synchronization_Exception)
 ~Guard () throw ()


Detailed Description

template<class LOCK> class ZThread::Guard

Author:
Eric Crahen
Version:
1.2.1
Date:
01-10-2001

Scoped locking utility. This template class can be given a Lockable synchronization object and can 'Guard' or serialize access to that method.

For instance, consider a case in which a class or program have a Mutex object associated with it. Access can be serialized with a Guard as shown below.

 Mutex _mtx;
 void guarded() {
 
    Guard<Mutex> g(_mtx);

 }

The Guard will lock the synchronization object when it is created and automatically unlock it when it goes out of scope. This eliminates common mistakes like forgetting to unlock your mutex.

An alternative to the above example would be

 void guarded() {

     (Guard<Mutex>)(_mtx);

 }

HOWEVER; using a Guard in this method is dangerous. Depending on your compiler an anonymous variable like this can go out of scope immediately which can result in unexpected behavior. - This is the case with MSVC and was the reason for introducing assertions into the Win32_MutexImpl to track this problem down


Constructor & Destructor Documentation

Guard ( LOCK & l ) [inline]
 

Lock a given Lockable object with the creation of this Guard

Parameters:
LOCK   & reference to a Lockable object
Parameters:
l  

Guard ( LOCK & l,
unsigned long timeout ) throw (Synchronization_Exception) [inline]
 

Lock a given Lockable object with the tryAcquire function

Parameters:
LOCK   & reference to a Lockable object
unsigned   long - milisecond timeout
Exceptions:
Timeout_Exception   - thrown if the tryAcquire operation times out
Parameters:
l  
timeout  

~Guard ( ) throw () [inline]
 

Unlock a given Lockable object with the destruction of this Guard


The documentation for this class was generated from the following file:
Generated at Fri Aug 31 09:08:00 2001 for ZThread by doxygen1.2.8 written by Dimitri van Heesch, © 1997-2001