|
OpenTop 1.3 | |||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | Cross-Platform C++ | ||||
SUMMARY: CONSTRUCTOR | METHOD | DETAIL: CONSTRUCTOR | METHOD |
#include "ot/base/AutoLock.h"
Acquires a lock on initialization and releases it on destruction.
AutoLock is based on the principle described by Bjarne Stroustrup as "resource acquisition is initialization". It is designed to lock and unlock synchronization objects in an exception-safe fashion by using local objects.In normal use, the constructor acquires a lock. This lock is then held until the object is destroyed at the end of the containing scope.
bool Foo::synchronizedMethod() { AutoLock<Mutex> lock(m_mutex); // acquires a lock on m_mutex ... // state can be updated here return true; // m_mutex is unlocked on exit }
The lock can be released (and re-acquired) at any time. The general rule is that if the lock is still held when an AutoLock object is destroyed, it will automatically release the lock.
AutoLock is a template class. It requires classes of type T to expose just two public methods: T::lock() and T::unlock().
Constructor/Destructor Summary | |
AutoLock(T& lock) Constructor which takes a reference to lock and locks it. | |
AutoLock(T& lock, bool bInitialLock) Constructor which takes a reference to lock and locks it if bInitialLock is true. | |
~AutoLock() Destructor which releases the lock if it is still being held. |
Method Summary | |
void |
lock() Acquires the lock. |
void |
unlock() Releases the lock. |
Typedefs |
typedef T LockType
Constructor/Destructor Detail |
AutoLock(T& lock)
lock
- NullPointerException
- AutoLock(T& lock, bool bInitialLock)
lock
- bInitialLock
- NullPointerException
- ~AutoLock()
Method Detail |
void lock()
void unlock()
|
OpenTop 1.3 | |||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | Cross-Platform C++ | ||||
SUMMARY: CONSTRUCTOR | METHOD | DETAIL: CONSTRUCTOR | METHOD |