Cross-Platform C++

ot
class AutoUnlock< T >

#include "ot/base/AutoUnlock.h"

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

In normal use, the constructor releases a lock. This lock is then free until the object is destroyed at the end of the current scope, when it is re-acquired

    bool Foo::waitMethod()
    {
        AutoUnlock<Mutex> lock(m_mutex); // releases the lock on m_mutex
        ...                              // activity that does not require the lock
        return true;                     // m_mutex is re-locked on exit
    }

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

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




Constructor/Destructor Summary
AutoUnlock(T& lock)
         Constructor which takes a reference to lock and unlocks it.
~AutoUnlock()
         Destructor which re-acquires the lock if it is not being held.

Method Summary
 void lock()
         Re-acquires the lock if it is not being held; has no effect otherwise.
 void unlock()
         Releases the lock if it is being held; has no effect otherwise.

Constructor/Destructor Detail

AutoUnlock

 AutoUnlock(T& lock)
Constructor which takes a reference to lock and unlocks it.

Parameters:
lock - a reference to the synchronization object that will be managed by this AutoUnlock object.

~AutoUnlock

 ~AutoUnlock()
Destructor which re-acquires the lock if it is not being held.


Method Detail

lock

void lock()
Re-acquires the lock if it is not being held; has no effect otherwise.


unlock

void unlock()
Releases the lock if it is being held; has no effect otherwise.



Cross-Platform C++

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

Copyright © 2000-2003 ElCel Technology   Trademark Acknowledgements