Jive reference manual
List of all members | Public Member Functions
jem::Lock< M > Class Template Reference

Locks and unlocks mutual exclusion objects. More...

#include <jem/base/Lock.h>

Public Member Functions

 Lock (const M &mutex)
 Locks a mutual exclusion object. More...
 
 ~Lock ()
 Unlocks the mutual exclusion object. More...
 

Detailed Description

template<class M>
class jem::Lock< M >

The class Lock can be instantiated for all types M that declare the member functions lock() and unlock(). Here is an example:

Mutex mutex;
// ...
{
Lock<Mutex> lock( mutex );
// execute atomic operations here
}

Because the destructor of the Lock class automatically unlocks the mutual exclusion object, it allows one to lock such an object in an exception safe way.

See also
Monitor, Mutex, ReadWriteMutex, RecursiveMonitor, RecursiveMutex.

Constructor & Destructor Documentation

template<class M >
jem::Lock< M >::Lock ( const M &  mutex)
explicit

The constructor of the Lock class locks a mutual exclusion object by calling its lock() member function. The constructor casts away the constness of the mutual exclusion variable so that the lock() member does not have to be declared const. The reason for declaring the parameter mutex as const is that a lock/unlock pair does not logically change the state of the mutex.

Parameters
mutex- the mutual exclusion object to be locked. The lifetime this object must exceed the lifetime of this Lock object.
template<class M >
jem::Lock< M >::~Lock ( )

The destructor of the Lock class calls the unlock() member of the mutual exclusion object that was passed as the parameter mutex in the constructor.