Webmaster  |  Imprint 
C++ Server Pages
Main  |  License  |  Documentation  |  Download 

MutexLock Class Reference

MutexLock class for Mutex. More...

#include <cxxtools/mutex.h>

Inheritance diagram for MutexLock:

List of all members.

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.


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 <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

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.

Copyright © 2008 The Tntnet Development Team
Tntnet 1.6