The Micro C++ Library
|
A Synchronized is used to automatically lock and unlock a mutex. More...
#include <micxx.hxx>
Inherits micxx::traits::NonCopyable.
Public Member Functions | |
Synchronized (Mutex &mutex) | |
Construct a Synchronized for Mutex mutex. | |
Synchronized (Mutex *mutex=0) | |
Construct a Synchronized for Mutex mutex. | |
virtual | ~Synchronized () |
If not released before by a call to release(), the stored Mutex reference is unlocked. | |
virtual void | release () |
Unlock and release the stored Mutex reference. No more action is done on this Mutex reference in ~Synchronized(). |
A Synchronized is used to automatically lock and unlock a mutex.
A Synchronized takes a mutex within its constructor and tries to receive its lock before the constructor returns. This guarded lock is automatically released during the destructor, if not released explicitly before by release().
Use a Synchronized on the stack to ensure that a mutex is locked after the instance was allocated on the stack and is definitely unlocked when the Synchronized instance is removed from the stack due to scope removal when leaving a block or method body.
#include <micxx.hxx> void foo() { micxx::Mutex mutex; micxx::Synchronized lock(mutex); // lock mutex ... } // unlock mutex autom.
micxx::Synchronized::Synchronized | ( | Mutex & | mutex | ) |
Construct a Synchronized for Mutex mutex.
The constructor does not return, before it has successfully locked the Mutex mutex. If mutex could not be successfully locked an exception is thrown.
mutex | The Mutex to be locked. |
micxx::Synchronized::Synchronized | ( | Mutex * | mutex = 0 | ) |
Construct a Synchronized for Mutex mutex.
If mutex is not 0
, the constructor does not return, before it has successfully locked mutex. If mutex could not be successfully locked an exception is thrown. If mutex is 0
, nothing is done.
mutex | The Mutex to be locked, or 0 . |
virtual micxx::Synchronized::~Synchronized | ( | ) | [virtual] |
virtual void micxx::Synchronized::release | ( | ) | [virtual] |
Unlock and release the stored Mutex reference. No more action is done on this Mutex reference in ~Synchronized().