The Micro C++ Library
micxx::Synchronized Class Reference

A Synchronized is used to automatically lock and unlock a mutex. More...

#include <micxx.hxx>

Inherits micxx::traits::NonCopyable.

List of all members.

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().

Detailed Description

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.
Author:
Norbert Klose
Date:
March, 2007

Constructor & Destructor Documentation

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.

Parameters:
mutexThe 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.

Parameters:
mutexThe Mutex to be locked, or 0.
virtual micxx::Synchronized::~Synchronized ( ) [virtual]

If not released before by a call to release(), the stored Mutex reference is unlocked.


Member Function Documentation

virtual void micxx::Synchronized::release ( ) [virtual]

Unlock and release the stored Mutex reference. No more action is done on this Mutex reference in ~Synchronized().


The documentation for this class was generated from the following file: