ZThread::Guard< LockType, LockingPolicy > Class Template Reference

#include <Guard.h>

Inheritance diagram for ZThread::Guard< LockType, LockingPolicy >:

ZThread::LockHolder< LockType > ZThread::NonCopyable List of all members.

Public Member Functions

 Guard (LockType &lock)
 Guard (LockType &lock, unsigned long timeout)
template<class U, class V>
 Guard (Guard< U, V > &g)
 Guard (Guard &g)
template<class U, class V>
 Guard (Guard< U, V > &g, LockType &lock)
 Guard (Guard &g, LockType &lock)
 ~Guard () throw ()

Detailed Description

template<class LockType, class LockingPolicy = LockedScope>
class ZThread::Guard< LockType, LockingPolicy >

Author:
Eric Crahen <http://www.code-foo.com>
Date:
<2003-07-16T17:55:42-0400>
Version:
2.2.0
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

template<class LockType, class LockingPolicy = LockedScope>
ZThread::Guard< LockType, LockingPolicy >::Guard ( LockType &  lock  )  [inline]

Create a Guard that enforces a the effective protection scope throughout the lifetime of the Guard object or until the protection scope is modified by another Guard.

Parameters:
lock LockType the lock this Guard will use to enforce its protection scope.
Postcondition:
the protection scope may be ended prematurely

template<class LockType, class LockingPolicy = LockedScope>
ZThread::Guard< LockType, LockingPolicy >::Guard ( LockType &  lock,
unsigned long  timeout 
) [inline]

Create a Guard that enforces a the effective protection scope throughout the lifetime of the Guard object or until the protection scope is modified by another Guard.

Parameters:
lock LockType the lock this Guard will use to enforce its protection scope.
Postcondition:
the protection scope may be ended prematurely

template<class LockType, class LockingPolicy = LockedScope>
template<class U, class V>
ZThread::Guard< LockType, LockingPolicy >::Guard ( Guard< U, V > &  g  )  [inline]

Create a Guard that shares the effective protection scope from the given Guard to this Guard.

Parameters:
g Guard<U, V> guard that is currently enabled
lock LockType the lock this Guard will use to enforce its protection scope.

template<class LockType, class LockingPolicy = LockedScope>
ZThread::Guard< LockType, LockingPolicy >::Guard ( Guard< LockType, LockingPolicy > &  g  )  [inline]

Create a Guard that shares the effective protection scope from the given Guard to this Guard.

Parameters:
g Guard guard that is currently enabled
lock LockType the lock this Guard will use to enforce its protection scope.

template<class LockType, class LockingPolicy = LockedScope>
template<class U, class V>
ZThread::Guard< LockType, LockingPolicy >::Guard ( Guard< U, V > &  g,
LockType &  lock 
) [inline]

Create a Guard that transfers the effective protection scope from the given Guard to this Guard.

Parameters:
g Guard<U, V> guard that is currently enabled
lock LockType the lock this Guard will use to enforce its protection scope.

template<class LockType, class LockingPolicy = LockedScope>
ZThread::Guard< LockType, LockingPolicy >::Guard ( Guard< LockType, LockingPolicy > &  g,
LockType &  lock 
) [inline]

Create a Guard that transfers the effective protection scope from the given Guard to this Guard.

Parameters:
g Guard guard that is currently enabled
lock LockType the lock this Guard will use to enforce its protection scope.

template<class LockType, class LockingPolicy>
ZThread::Guard< LockType, LockingPolicy >::~Guard (  )  throw ()

Unlock a given Lockable object with the destruction of this Guard


The documentation for this class was generated from the following file:
Generated on Wed Aug 30 03:27:48 2006 for SWG EMU by  doxygen 1.4.7