Cross-Platform C++

ot
class AutoLock< T >

#include "ot/base/AutoLock.h"

Acquires a lock on initialization and releases it on destruction. AutoLock is based on the principle described by Bjarne Stroustrup as "resource acquisition is initialization". It is designed to lock and unlock synchronization objects in an exception-safe fashion by using local objects.

In normal use, the constructor acquires a lock. This lock is then held until the object is destroyed at the end of the containing scope.

    bool Foo::synchronizedMethod()
    {
        AutoLock<Mutex> lock(m_mutex); // acquires a lock on m_mutex
        ...                            // state can be updated here
        return true;                   // m_mutex is unlocked on exit
    }

The lock can be released (and re-acquired) at any time. The general rule is that if the lock is still held when an AutoLock object is destroyed, it will automatically release the lock.

AutoLock is a template class. It requires classes of type T to expose just two public methods: T::lock() and T::unlock().




Constructor/Destructor Summary
AutoLock(T& lock)
         Constructor which takes a reference to lock and locks it.
AutoLock(T& lock, bool bInitialLock)
         Constructor which takes a reference to lock and locks it if bInitialLock is true.
~AutoLock()
         Destructor which releases the lock if it is still being held.

Method Summary
 void lock()
         Acquires the lock.
 void unlock()
         Releases the lock.

Typedefs

LockType

typedef T LockType

Constructor/Destructor Detail

AutoLock

 AutoLock(T& lock)
Constructor which takes a reference to lock and locks it.

Parameters:
lock - a reference to the synchronization object that will be managed by this AutoLock object.
Exceptions:
NullPointerException - if lock is a reference to a null pointer

AutoLock

 AutoLock(T& lock,
          bool bInitialLock)
Constructor which takes a reference to lock and locks it if bInitialLock is true.

Parameters:
lock - a reference to the synchronization object that will be managed by this AutoLock object.
bInitialLock - if true, the lock is acquired by the constructor
Exceptions:
NullPointerException - if lock is a reference to a null pointer

~AutoLock

 ~AutoLock()
Destructor which releases the lock if it is still being held.


Method Detail

lock

void lock()
Acquires the lock. If the lock is already held this method has no effect.


unlock

void unlock()
Releases the lock. If the lock is not currently being held, this method has no effect.



Cross-Platform C++

Found a bug or missing feature? Please email us at support@elcel.com

Copyright © 2000-2003 ElCel Technology   Trademark Acknowledgements