cxxtools::MutexLock Class Reference

MutexLock class for Mutex. More...

#include <cxxtools/mutex.h>

Inheritance diagram for cxxtools::MutexLock:
cxxtools::NonCopyable

Public Member Functions

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

Additional Inherited Members

- Private Member Functions inherited from cxxtools::NonCopyable
 NonCopyable ()

Detailed Description

MutexLock class for Mutex.

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 <cxxtools/System/Mutex.h>
class MyClass
{
public:
void function()
{
MutexLock lock(_mtx);
//
// protected operations
//
// dtor of MutexLock unlocks _mtx
}
private:
cxxtools::System::Mutex _mtx;
};

Constructor & Destructor Documentation

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

Construct to guard a Mutex.

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.

cxxtools::MutexLock::~MutexLock ( )
inline

Unlocks the mutex unless unlock() was called.

Member Function Documentation

void cxxtools::MutexLock::lock ( )
inline
Mutex& cxxtools::MutexLock::mutex ( )
inline

Returns the guarded the mutex object.

const Mutex& cxxtools::MutexLock::mutex ( ) const
inline

Returns the guarded the mutex object.

void cxxtools::MutexLock::unlock ( )
inline

Unlock so that the destructor does not unlock.


The documentation for this class was generated from the following file: