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. More... | |
ScopedRWLock (ReadWriteLock *rwlock, LockType lock_type=LOCK_WRITE, bool initially_lock=true) | |
Constructor. More... | |
~ScopedRWLock () | |
Destructor. More... | |
void | relock () |
Lock this rwlock, again. More... | |
void | unlock () |
Unlock the rwlock. More... | |
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.
Enumerator | |
---|---|
LOCK_WRITE | Lock for writing. |
LOCK_READ | Lock for reading. |
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 LOCK_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_read(), fawkes::ReadWriteLock::lock_for_write(), and LOCK_WRITE.
fawkes::ScopedRWLock::~ScopedRWLock | ( | ) |
Destructor.
Definition at line 135 of file scoped_rwlock.cpp.
References fawkes::ReadWriteLock::unlock().
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.
References fawkes::ReadWriteLock::lock_for_read(), fawkes::ReadWriteLock::lock_for_write(), and LOCK_WRITE.
void fawkes::ScopedRWLock::unlock | ( | ) |
Unlock the rwlock.
Definition at line 172 of file scoped_rwlock.cpp.
References fawkes::ReadWriteLock::unlock().