#include <Guard.h>
Public Methods | |
Guard (LOCK &l) | |
Guard (LOCK &l, unsigned long timeout) throw (Synchronization_Exception) | |
~Guard () throw () |
For instance, consider a case in which a class or program have a Mutex object associated with it. Access can be serialized with a Guard as shown below.
Mutex _mtx; void guarded() { Guard<Mutex> g(_mtx); }
The Guard will lock the synchronization object when it is created and automatically unlock it when it goes out of scope. This eliminates common mistakes like forgetting to unlock your mutex.
An alternative to the above example would be
void guarded() { (Guard<Mutex>)(_mtx); }
HOWEVER; using a Guard in this method is dangerous. Depending on your compiler an anonymous variable like this can go out of scope immediately which can result in unexpected behavior. - This is the case with MSVC and was the reason for introducing assertions into the Win32_MutexImpl to track this problem down
|
Lock a given Lockable object with the creation of this Guard
|
|
Lock a given Lockable object with the tryAcquire function
|
|
Unlock a given Lockable object with the destruction of this Guard |