threadmm::lock_guard< Lock > Class Template Reference
[Utility classes]

#include <threadmm/lock_guard.hh>

Collaboration diagram for threadmm::lock_guard< Lock >:

Collaboration graph
[legend]
List of all members.

Detailed Description

template<typename Lock>
class threadmm::lock_guard< Lock >

Lock_Guard class used to lock mutex's.

Parameters:
Lock The kind of locks to use.
This class is useful for writting thread-safe code. One creates a lock_guard object on the stack on entry into the critical section, passing it a mutex to lock. When the lock_guard object goes out of scope, the mutex is automatically released. This is a fairly well-known trick to use. Here's an example for a class fulfilling a Singleton pattern, were we protect the singleton creation from race conditions in the case mutliple threads call the singleton::instance() member function:
      #ifdef _REENTRANT
      #define MUTEX_TYPE threadmm::mutex
      #else 
      #define MUTEX_TYPE threadmm::mutex_dummy
      #endif

      template <typename Mutex=MUTEX_TYPE>
      class singleton {
      private: 
        static Mutex      _mutex;  
        static singleton* _instance; 
      public: 
        singleton() {}
        static singleton* instance() { 
          if (!_instance) { 
            threadmm::lock_guard g(_mutex); 
            if (!_instance) _instance = new singleton;
          }
          return _instance;
        }
      };
In a single threaded application, the user can pass some dummy mutex class as the template argument, and suffer no penalties.
Examples:

barrier_sync.cc, and prod_cons_cond.cc.


Public Types

typedef Lock lock_type

Public Member Functions

 lock_guard (lock_type &l, bool spin=false)
 lock_guard (volatile lock_type &l, bool spin=false)
virtual ~lock_guard ()
void lock (bool spin=false)
void lock (bool spin=false) volatile
void unlock ()

Protected Attributes

lock_type_lock
bool _released


Member Typedef Documentation

template<typename Lock>
typedef Lock threadmm::lock_guard< Lock >::lock_type
 

Type of the lock.


Constructor & Destructor Documentation

template<typename Lock>
threadmm::lock_guard< Lock >::lock_guard lock_type l,
bool  spin = false
[inline]
 

Constructor.

Locks the passed lock. See also lock member function.

Parameters:
l The lock to use with this lock_guard.
spin see lock member function.

template<typename Lock>
threadmm::lock_guard< Lock >::lock_guard volatile lock_type l,
bool  spin = false
[inline]
 

Constructor.

Locks the passed lock. See also lock member function.

Parameters:
l The lock to use with this lock_guard.
spin see lock member function.

template<typename Lock>
virtual threadmm::lock_guard< Lock >::~lock_guard  )  [inline, virtual]
 

Destructor.

Unlocks the stored lock.


Member Function Documentation

template<typename Lock>
void threadmm::lock_guard< Lock >::lock bool  spin = false  )  volatile
 

Lock the lock passed to the constructor.

Parameters:
spin If true, the guard will loop indefently until it gets a lock (via Lock::try_lock member function). Otherwise, it will attempt a normal lock.

template<typename Lock>
void threadmm::lock_guard< Lock >::lock bool  spin = false  ) 
 

Lock the lock passed to the constructor.

Parameters:
spin If true, the guard will loop indefently until it gets a lock (via Lock::try_lock member function). Otherwise, it will attempt a normal lock.

template<typename Lock>
void threadmm::lock_guard< Lock >::unlock  )  [inline]
 

Unlock the lock passed to the constructor.


Member Data Documentation

template<typename Lock>
lock_type& threadmm::lock_guard< Lock >::_lock [protected]
 

The lock to use.

template<typename Lock>
bool threadmm::lock_guard< Lock >::_released [protected]
 

Flag.

True if lock was released (by unlock member function) before the deconstructor was reached (scope ended).


The documentation for this class was generated from the following file:
Top of page
Last update Tue Nov 9 12:40:51 2004
Christian Holm
Created by DoxyGen 1.3.9.1