MutexLock Class Reference

#include <Pt/System/Mutex.h>

MutexLock class for Mutex. More...

Inherits NonCopyable.

Public Member Functions

 MutexLock (Mutex &m, bool doLock=true, bool isLocked=false)
 Construct to guard a Mutex. More...
 
 ~MutexLock ()
 Unlocks the mutex unless unlock() was called.
 
void lock ()
 Lock the mutex.
 
Mutexmutex ()
 Returns the guarded the mutex object.
 
const Mutexmutex () const
 Returns the guarded the mutex object.
 
void unlock ()
 Unlock so that the destructor does not unlock.
 

Detailed Description

The MutexLock class adds functionality for scoped locking. In the constructor of a MutexLock, the mutex is locked and in the destructor it is unlocked. This way if for example an exception occures in the protected section the Mutex will be unlocked during stack unwinding when the MutexLock is destructed.

// example how to make a member function thread-safe
#include <Pt/System/Mutex.h>
class MyClass
{
public:
void function()
{
MutexLock lock(_mtx);
//
// protected operations
//
// dtor of MutexLock unlocks _mtx
}
private:
};

Constructor & Destructor Documentation

MutexLock ( Mutex m,
bool  doLock = true,
bool  isLocked = false 
)

Constructs a MutexLock object and locks the enclosing mutex if doLock is true. If isLocked is true, the MutexLock will only unlock the given mutex in the destructor, but not lock it in the constructor.