CXScopeLock

From cxwiki

The CXScopeLock template class is intended to be constructed on the stack as a mechanism to take a lock and ensure that it is released again when the CXScopeLock object goes out of scope. This is intended as a way to reduce manual lock management errors.
 
// Construct a CXScopeLock object, taking a lock on the specified mutex.
CXScopeLock(const T& mutex);

// Construct a CXScopeLock object, optionally taking a lock on the specified mutex.
CXScopeLock(const T& mutex, bool bShouldTakeLock);

// Destruct a CXScopeLock object, releasing any outstanding lock held by this 
// object.
~CXScopeLock(void);

// Take a lock on the specified mutex. This is a no-op if the specified mutex is 
// already locked. This results in any outstanding mutex lock held by this object
// being releasing first.
void Lock(const T& mutex);

// Unlock any outstanding lock held by this object.
void Unlock(void);

// Detach from any outstanding locks without releasing them. It is the 
// responsibility of the caller to determine whether anything was locked and
// release that lock when necessary.
void Detach(void);

Restrictions

The template parameter must be a lock type with the same interface as CXMutex. This includes CXRecursiveMutex and CXSpinLock but not CXReadWriteLock. A separate CXScopeLockReadWrite is available.