ScopedLock Class Reference
[Scoped]

#include <ScopedLock.h>

List of all members.


Detailed Description

provides a scoped use of a lock

Acquires the lock in the constructor, releases the lock in the destructor. Thus properly unlocks (even for recursive locks) in the case of exceptions, return statements, etc., without having to worry about releasing the lock.

Also supports programmatic releasing and acquiring of the lock, and will make sure the lock is unlocked with the object goes out of scope.

Common usage:

 // for some pthread_mutex_t m
 ScopedLock sl(m);
 // some stuff
 sl.unlock();
 // stuff without the lock
 if (!keepgoing) {
   return;
 }
 sl.lock();
 // some more stuff

Todo:
consider placing ScopedLock in the mace namespace.

Definition at line 76 of file ScopedLock.h.


Public Member Functions

 ScopedLock (pthread_mutex_t &l)
 locks the mutex passed in the constructor, holds the lock until destruction
void lock ()
 acquire the lock, and ensure unlocking it when the object is destructed
void unlock ()
 release the lock, ensuring not re-unlocking it when the object is destructed

Protected Attributes

bool locked
 stores whether the lock is held by this object or not.
pthread_mutex_t & slock
 reference to the lock of this object.

Member Function Documentation

void ScopedLock::lock (  )  [inline]

acquire the lock, and ensure unlocking it when the object is destructed

Precondition:
!locked
Postcondition:
locked

Definition at line 94 of file ScopedLock.h.

References ASSERT, locked, and slock.

Referenced by ScopedLock().

void ScopedLock::unlock (  )  [inline]

release the lock, ensuring not re-unlocking it when the object is destructed

Precondition:
locked
Postcondition:
!locked

Definition at line 105 of file ScopedLock.h.

References ASSERT, locked, and slock.

Referenced by Util::getAddr().


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

Generated on Sat Oct 4 21:30:30 2008 for Mace Library by  doxygen 1.5.5