Vaca::ScopedLock Class Reference

#include <ScopedLock.h>

Inheritance diagram for Vaca::ScopedLock:

Vaca::NonCopyable

List of all members.


Detailed Description

An object to safely lock and unlock mutexes.

The constructor of ScopedLock locks the mutex, the destructor unlocks the mutex. In this way you can safely use ScopedLock inside a try/catch block without worrying about to the lock state of the mutex.

For example:

 try {
   ScopedLock hold(mutex);
   throw Exception();
 }
 catch (...) {
   // the mutex is unlocked here
 }
 // if you don't throw a exception, the mutex is unlocked here too

See also:
Mutex, ConditionVariable

Public Member Functions

 ScopedLock (Mutex &mutex)
 Creates the ScopedLock locking the specified mutex.
 ~ScopedLock ()
 Destroys the ScopedLock unlocking the held mutex.
MutexgetMutex () const
 Returns which mutex is being held.

Private Member Functions

 ScopedLock ()

Private Attributes

Mutexm_mutex

Constructor & Destructor Documentation

Vaca::ScopedLock::ScopedLock (  )  [private]

Vaca::ScopedLock::ScopedLock ( Mutex mutex  )  [inline]

Creates the ScopedLock locking the specified mutex.

Parameters:
mutex Mutex to be hold by the ScopedLock's life-time.

Vaca::ScopedLock::~ScopedLock (  )  [inline]

Destroys the ScopedLock unlocking the held mutex.


Member Function Documentation

Mutex& Vaca::ScopedLock::getMutex (  )  const [inline]

Returns which mutex is being held.


Member Data Documentation