C12Adapter Opensource C++ Interface
MSynchronizer::Locker Class Reference

Scope based locker that acquires the lock at construction, and releases it at destruction. More...

Public Member Functions

 Locker (const MSynchronizer &s)
 Lock the given object at construction. More...
 
 Locker (const MSynchronizer &s, long timeout)
 Lock the given object with timeout at construction. More...
 
 ~Locker ()
 If the lock was acquired at construction, unlock the client synchronizer at destruction. More...
 
bool IsLocked () const
 Whether the object has been locked in the constructor. More...
 

Detailed Description

Scope based locker that acquires the lock at construction, and releases it at destruction.

Typical usage of this convenience class is

{
MSynchronizer::Locker lock(criticalSection); // acquire the lock on the critical section
// Any code here that can possibly throw an exception
} // Lock is released whether or not the above code threw an exception

Constructor & Destructor Documentation

MSynchronizer::Locker::Locker ( const MSynchronizer s)

Lock the given object at construction.

If the object is already locked within a different thread, the constructor call will wait until the lock is released.

Lock timeout is set to infinity so that if the constructor succeeds, IsLocked() will always return true.

The constructor can fail with a system error exception.

MSynchronizer::Locker::Locker ( const MSynchronizer s,
long  timeout 
)

Lock the given object with timeout at construction.

If the object is already locked within a different thread, the constructor will wait for the given number of milliseconds until the lock is released.

If the given timeout expires the constructor exits successfully, but IsLocked() will return false.

The constructor can fail with a system error exception.

MSynchronizer::Locker::~Locker ( )

If the lock was acquired at construction, unlock the client synchronizer at destruction.

Typically, the destructor is called implicitly when the locker object goes out of scope.

Member Function Documentation

bool MSynchronizer::Locker::IsLocked ( ) const
inline

Whether the object has been locked in the constructor.

This is useful to determine if the timeout has occurred, in which case the return value will be false.