#include <Guard.h>
Inheritance diagram for ZThread::Guard< LockType, LockingPolicy >:
Public Member Functions | |
Guard (LockType &lock) | |
Guard (LockType &lock, unsigned long timeout) | |
template<class U, class V> | |
Guard (Guard< U, V > &g) | |
Guard (Guard &g) | |
template<class U, class V> | |
Guard (Guard< U, V > &g, LockType &lock) | |
Guard (Guard &g, LockType &lock) | |
~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
ZThread::Guard< LockType, LockingPolicy >::Guard | ( | LockType & | lock | ) | [inline] |
Create a Guard that enforces a the effective protection scope throughout the lifetime of the Guard object or until the protection scope is modified by another Guard.
lock | LockType the lock this Guard will use to enforce its protection scope. |
ZThread::Guard< LockType, LockingPolicy >::Guard | ( | LockType & | lock, | |
unsigned long | timeout | |||
) | [inline] |
Create a Guard that enforces a the effective protection scope throughout the lifetime of the Guard object or until the protection scope is modified by another Guard.
lock | LockType the lock this Guard will use to enforce its protection scope. |
ZThread::Guard< LockType, LockingPolicy >::Guard | ( | Guard< U, V > & | g | ) | [inline] |
ZThread::Guard< LockType, LockingPolicy >::Guard | ( | Guard< LockType, LockingPolicy > & | g | ) | [inline] |
ZThread::Guard< LockType, LockingPolicy >::Guard | ( | Guard< U, V > & | g, | |
LockType & | lock | |||
) | [inline] |
ZThread::Guard< LockType, LockingPolicy >::Guard | ( | Guard< LockType, LockingPolicy > & | g, | |
LockType & | lock | |||
) | [inline] |
ZThread::Guard< LockType, LockingPolicy >::~Guard | ( | ) | throw () |