|
OpenTop 1.3 | |||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | Cross-Platform C++ | ||||
SUMMARY: CONSTRUCTOR | METHOD | DETAIL: CONSTRUCTOR | METHOD |
#include "ot/base/SynchronizedObject.h"
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 |
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 |
protected void lock()
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..
protected void unlock()
|
OpenTop 1.3 | |||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | Cross-Platform C++ | ||||
SUMMARY: CONSTRUCTOR | METHOD | DETAIL: CONSTRUCTOR | METHOD |