#include <ScopedLock.h>
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
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. |
void ScopedLock::lock | ( | ) | [inline] |
acquire the lock, and ensure unlocking it when the object is destructed
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
Definition at line 105 of file ScopedLock.h.
References ASSERT, locked, and slock.
Referenced by Util::getAddr().