Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members  

Guard< Lock > Class Template Reference

List of all members.

Detailed Description

template<class Lock = Mutex>
class Guard< Lock >

A class which acquires a lock on creating and destroys it on destruction.

It is typically used like so:

 Mutex my_mutex;

 int my_function()
 {
     Guard g(my_mutex);
     // now Mutex is acquired

     do_some_exclusive_stuff();
     if (some_condition())
         return 1;
     do_other_stuff();

     return 0;
 }

The Mutex is automatically released whenever the Guard goes out of scope, so you don't need to worry about releasing the Mutex in case of exception (e.g., in do_some_exclusive_stuff) or if a code block ends early (e.g., returns 1 in that if clause).

The above function is actually better written using the locking(x) macro.


Public Methods

 Guard (const Guard &g)
Guard & operator= (const Guard &g)
 Guard (Lock &m)
 Guard (Lock *m)
 ~Guard ()
 operator bool () const


Constructor & Destructor Documentation

template<class Lock = Mutex>
Guard< Lock >::Guard const Guard< Lock > &    g
 

template<class Lock = Mutex>
Guard< Lock >::Guard Lock &    m
 

template<class Lock = Mutex>
Guard< Lock >::Guard Lock *    m
 

template<class Lock = Mutex>
Guard< Lock >::~Guard  
 


Member Function Documentation

template<class Lock = Mutex>
Guard< Lock >::operator bool   const
 

template<class Lock = Mutex>
Guard& Guard< Lock >::operator= const Guard< Lock > &    g
 


The documentation for this class was generated from the following file:
Generated on Fri Dec 20 13:35:11 2002 for NMSTL