|
Fawkes API
Fawkes Development Version
|
Scoped read/write lock. More...
#include <>>
Public Types | |
| enum | LockType { LOCK_WRITE, LOCK_READ } |
| What to lock for. More... | |
Public Member Functions | |
| ScopedRWLock (RefPtr< ReadWriteLock > rwlock, LockType lock_type=LOCK_WRITE, bool initially_lock=true) | |
| Constructor. | |
| ScopedRWLock (ReadWriteLock *rwlock, LockType lock_type=LOCK_WRITE, bool initially_lock=true) | |
| Constructor. | |
| ~ScopedRWLock () | |
| Destructor. | |
| void | relock () |
| Lock this rwlock, again. | |
| void | unlock () |
| Unlock the rwlock. | |
Scoped read/write lock.
This class is a convenience class which can help you prevent quite a few headaches. Consider the following code.
This is not a complete list of examples but as you see if you have many exit points in a function it becomes more and more work to have correct locking behavior.
This is a lot simpler with the ScopedRWLock. The ScopedRWLock locks the given ReadWriteLock on creation, and unlocks it in the destructor. If you now have a read/write locker on the stack as integral type the destructor is called automagically on function exit and thus the lock is appropriately unlocked. The code would look like this:
Definition at line 33 of file scoped_rwlock.h.
What to lock for.
Definition at line 37 of file scoped_rwlock.h.
| fawkes::ScopedRWLock::ScopedRWLock | ( | RefPtr< ReadWriteLock > | rwlock, |
| ScopedRWLock::LockType | lock_type = LOCK_WRITE, |
||
| bool | initially_lock = true |
||
| ) |
Constructor.
| rwlock | ReadWriteLock to lock/unlock appropriately. |
| initially_lock | true to lock the rwlock in the constructor, false to not lock |
| lock_type | locking type, lock either for writing or for reading |
Definition at line 95 of file scoped_rwlock.cpp.
References fawkes::ReadWriteLock::lock_for_write().
| fawkes::ScopedRWLock::ScopedRWLock | ( | ReadWriteLock * | rwlock, |
| ScopedRWLock::LockType | lock_type = LOCK_WRITE, |
||
| bool | initially_lock = true |
||
| ) |
Constructor.
| rwlock | ReadWriteLock to lock/unlock appropriately. |
| initially_lock | true to lock the rwlock in the constructor, false to not lock |
| lock_type | locking type, lock either for writing or for reading |
Definition at line 118 of file scoped_rwlock.cpp.
References fawkes::ReadWriteLock::lock_for_write().
| fawkes::ScopedRWLock::~ScopedRWLock | ( | ) |
Destructor.
Definition at line 135 of file scoped_rwlock.cpp.
| void fawkes::ScopedRWLock::relock | ( | ) |
Lock this rwlock, again.
Use this if you unlocked the rwlock from the outside.
Definition at line 151 of file scoped_rwlock.cpp.
| void fawkes::ScopedRWLock::unlock | ( | ) |
Unlock the rwlock.
Definition at line 172 of file scoped_rwlock.cpp.