HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_DoubleLock< T, LOCK_T > Class Template Reference

#include <UT_DoubleLock.h>

+ Inheritance diagram for UT_DoubleLock< T, LOCK_T >:

Public Member Functions

 UT_DoubleLock (LOCK_T &lock, volatile T &val)
 
 ~UT_DoubleLock ()
 
T getValue ()
 
void setValue (T val)
 
void abort ()
 

Detailed Description

template<typename T, typename LOCK_T = UT_Lock>
class UT_DoubleLock< T, LOCK_T >

A double-checked lock. Only locks the thread lock when the value is 0.

To use this lock:

static UT_Lock theLock; static OBJECT *theObj = 0;

OBJECT * getSingleton() { UT_DoubleLock<OBJECT *> lock(theLock, theObj);

if (!lock.getValue()) { // NOTE: This doesn't set theObj. theObj is set when // destructing lock. lock.setValue(new OBJECT()); } return lock.getValue(); }

NOTE: DO NOT roll your own double checked lock! If you don't know when to use which memory fences and why, odds are high that your custom implementation will be thread-unsafe.

For simple singleton initialization, consider using UT_Singleton.

Examples:
packedshareddata/GU_PackedSharedData.C.

Definition at line 50 of file UT_DoubleLock.h.

Constructor & Destructor Documentation

template<typename T, typename LOCK_T = UT_Lock>
UT_DoubleLock< T, LOCK_T >::UT_DoubleLock ( LOCK_T &  lock,
volatile T val 
)
inline

Definition at line 53 of file UT_DoubleLock.h.

template<typename T, typename LOCK_T = UT_Lock>
UT_DoubleLock< T, LOCK_T >::~UT_DoubleLock ( )
inline

Definition at line 69 of file UT_DoubleLock.h.

Member Function Documentation

template<typename T, typename LOCK_T = UT_Lock>
void UT_DoubleLock< T, LOCK_T >::abort ( )
inline

Abort writing back out to the val passed in the constructor upon destruction.

Definition at line 120 of file UT_DoubleLock.h.

template<typename T, typename LOCK_T = UT_Lock>
T UT_DoubleLock< T, LOCK_T >::getValue ( )
inline

Definition at line 94 of file UT_DoubleLock.h.

template<typename T, typename LOCK_T = UT_Lock>
void UT_DoubleLock< T, LOCK_T >::setValue ( T  val)
inline

Definition at line 112 of file UT_DoubleLock.h.


The documentation for this class was generated from the following file: