Cross-Platform C++

ot
class SynchronizedObject

#include "ot/base/SynchronizedObject.h"

ot::ManagedObject ot::io::Reader ot::io::Writer ot::Monitor ot::net::SocketDescriptor ot::io::BufferedReader ot::io::FilterReader ot::io::InputStreamReader ot::io::StringReader ot::io::BufferedWriter ot::io::FilterWriter ot::io::OutputStreamWriter ot::io::PrintWriter ot::io::StringWriter ot::Thread A Base class facilitating synchronized concurrent access from multiple threads. The SynchronizedObject enables thread-safe access by the use of a contained RecursiveMutex object. Locking and unlocking of the mutex is performed by a friend class with the typedef name SynchronizedObject::Lock. This class is designed to be used as a scoped lock: the mutex lock is acquired by the constructor and released by the destructor. In this way, the mutex lock is guaranteed to be released even if an exception is thrown by the intervening code.

    int SafeObject::getSafeId()
    {
        // acquire our mutex lock
        SynchronizedObject::Lock lock(*this);
        return m_id;
        // mutex lock is automatically released at end of scope
    }

The SynchronizedObject::Lock constructor takes a non-const reference to a SynchronizedObject. To use SynchronizedObject::Lock from a const function or with a const object reference it is necessary to "cast away the constness". To avoid having to litter your const-correct code with casts, the OT_SYNCHRONIZED macro may be used. For example:

    int SafeObject::getSafeConstId() const
    {
        OT_SYNCHRONIZED
        return m_id;
    }

The OT_SYNCHRONIZED macro has the additional advantage that it is defined to do nothing in the single-threaded version of the library.

The SynchronizedObject class is derived from ManagedObject so that standard OpenTop reference-counted object references can be created and stored.

If you wish to synchronize against another SynchronizedObject, there are two other macros to choose from:-

    void someSynchronizedFunction()
    {
        OT_SYNCHRONIZED_PTR_ADD(getSyncObject())

        // Do something useful here
    }




Method Summary
protected  void lock()
         Locks the mutex.
protected  void unlock()
         Unlocks the mutex.

Methods inherited from class ot::ManagedObject
addRef, getRefCount, onFinalRelease, operator=, release

Typedefs

Lock

typedef AutoLock< SynchronizedObject > Lock

Type used to create scoped locks when the lifetime of the SynchronizedObject is guaranteed to exceed the locked scope.

Method Detail

lock

protected void lock()
Locks the mutex. If the mutex is already locked by another thread, the current thread blocks until the mutex becomes free.

While a thread has ownership of a SynchronizedObject, it can specify the same object in additional lock() calls without blocking its execution. This prevents a thread from deadlocking itself while waiting for a SynchronizedObject that it already owns. However, to release its ownership, the thread must call unlock() once for each time that the SynchronizedObject was locked..


unlock

protected void unlock()
Unlocks the mutex. Decrements the use count of the internal mutex. If the use count is decremented to zero, the mutex is released and may then be acquired by another thread.



Cross-Platform C++

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

Copyright © 2000-2003 ElCel Technology   Trademark Acknowledgements