casa
$Rev:20696$
|
Thread-safe initialization of global variables. More...
#include <Mutex.h>
Public Types | |
typedef void | InitFunc (void *) |
Define the initialization function to call. | |
Public Member Functions | |
MutexedInit (InitFunc *func, void *arg=0, Mutex::Type type=Mutex::Auto) | |
Create the mutex and set that the initialization should be done. | |
void | exec () |
Execute the initialization function if not done yet. | |
Mutex & | mutex () |
Get the mutex (to make it possible to lock for other purposes). | |
Private Member Functions | |
MutexedInit (const MutexedInit &) | |
Forbid copy constructor. | |
MutexedInit & | operator= (const MutexedInit &) |
Forbid assignment. | |
void | doExec () |
Thread-safe execution of the initialization function (if still needed). | |
Private Attributes | |
Mutex | itsMutex |
InitFunc * | itsFunc |
void * | itsArg |
volatile Bool | itsDoExec |
Thread-safe initialization of global variables.
Public interface
This class does a double checked lock.
Often data needs to be initialized once and accessed many times. To do this in a thread-safe way, a mutex lock needs to be used. However, that is relatively expensive. The double checked lock idiom overcomes this problem. A Bool flag tells if an operation needs to be done. If so, a lock is set and the flag is tested again in case another thread happened to do the operation between the test and acquiring the lock. At the end of the operation the flag is cleared.
The flag needs to be declared volatile to avoid execution ordering problems in case a compiler optimizes too much.
Note: This idiom is not fully portable, because on more exotic machines the caches in different cores may not be synchronized well;
typedef void casa::MutexedInit::InitFunc(void *) |
casa::MutexedInit::MutexedInit | ( | InitFunc * | func, |
void * | arg = 0 , |
||
Mutex::Type | type = Mutex::Auto |
||
) |
Create the mutex and set that the initialization should be done.
casa::MutexedInit::MutexedInit | ( | const MutexedInit & | ) | [private] |
Forbid copy constructor.
void casa::MutexedInit::doExec | ( | ) | [private] |
Thread-safe execution of the initialization function (if still needed).
Referenced by exec().
void casa::MutexedInit::exec | ( | ) | [inline] |
Execute the initialization function if not done yet.
Definition at line 171 of file Mutex.h.
References doExec(), and itsDoExec.
Referenced by casa::MCPosition::fillState(), casa::MCDoppler::fillState(), casa::MCRadialVelocity::fillState(), casa::MCFrequency::fillState(), casa::MCEpoch::fillState(), casa::MCBaseline::fillState(), casa::MCuvw::fillState(), casa::MCEarthMagnetic::fillState(), casa::MCDirection::fillState(), casa::ColumnDesc::registerMainCtor(), and casa::DataManager::registerMainCtor().
Mutex& casa::MutexedInit::mutex | ( | ) | [inline] |
MutexedInit& casa::MutexedInit::operator= | ( | const MutexedInit & | ) | [private] |
Forbid assignment.
void* casa::MutexedInit::itsArg [private] |
volatile Bool casa::MutexedInit::itsDoExec [private] |
InitFunc* casa::MutexedInit::itsFunc [private] |
Mutex casa::MutexedInit::itsMutex [private] |