Fawkes API
Fawkes Development Version
|
#include <>>
Public Member Functions | |
MutexLocker (RefPtr< Mutex > mutex, bool initially_lock=true) | |
Constructor. More... | |
MutexLocker (Mutex *mutex, bool initially_lock=true) | |
Constructor. More... | |
~MutexLocker () | |
Destructor. More... | |
void | relock () |
Lock this mutex, again. More... | |
void | unlock () |
Unlock the mutex. More... | |
Mutex locking helper.
This class is a convenience function which can help you prevent a 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 MutexLocker. The MutexLocker locks the given mutex on creation, and unlocks it in the destructor. If you now have a mutex locker on the stack as integral type the destructor is called automagically on function exit and thus the mutex is appropriately unlocked. The code would look like this:
Definition at line 33 of file mutex_locker.h.
Constructor.
mutex | Mutex to lock/unlock appropriately. |
initially_lock | true to lock the mutex in the constructor, false to not lock |
Definition at line 90 of file mutex_locker.cpp.
fawkes::MutexLocker::MutexLocker | ( | Mutex * | mutex, |
bool | initially_lock = true |
||
) |
Constructor.
mutex | Mutex to lock/unlock appropriately. |
initially_lock | true to lock the mutex in the constructor, false to not lock |
Definition at line 105 of file mutex_locker.cpp.
References fawkes::Mutex::lock().
fawkes::MutexLocker::~MutexLocker | ( | ) |
void fawkes::MutexLocker::relock | ( | ) |
Lock this mutex, again.
Use this if you unlocked the mutex from the outside.
Definition at line 132 of file mutex_locker.cpp.
References fawkes::Mutex::lock().
Referenced by fawkes::LuaContext::restart().
void fawkes::MutexLocker::unlock | ( | ) |
Unlock the mutex.
Definition at line 145 of file mutex_locker.cpp.
References fawkes::Mutex::unlock().
Referenced by fawkes::FawkesNetworkServerThread::add_connection(), SickTiM55xUSBAcquisitionThread::loop(), fawkes::FawkesNetworkClientRecvThread::recv(), and fawkes::LuaContext::restart().