If you can read this text, it means you are not experiencing this website at its best. This website is designed for used with a standards-compliant browser.
Current version: 2.3.1
ZThreads
A platform-independent, multi-threading and synchronization library for C++
Home Documentation Downloads CVS Contact

# Guard Class Template Reference

`#include <Guard.h>`

Inheritance diagram for Guard:

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 ()
## Friends
class | LockHolder< LockType >

## 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

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

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

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.

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.

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.

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.

~Guard | ( |  | ) | throw ()

| Unlock a given Lockable object with the destruction of this Guard

The documentation for this class was generated from the following file:

- Guard.h