casacore
Public Types | Public Member Functions | Private Member Functions | Private Attributes | List of all members
casacore::MutexedInit Class Reference

Thread-safe initialization of global variables. More...

#include <Mutex.h>

Public Types

typedef void InitFunc(void *)
 Define the initialization function to call. More...
 

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. More...
 
void exec ()
 Execute the initialization function if not done yet. More...
 
Mutexmutex ()
 Get the mutex (to make it possible to lock for other purposes). More...
 

Private Member Functions

 MutexedInit (const MutexedInit &)
 Forbid copy constructor. More...
 
MutexedInitoperator= (const MutexedInit &)
 Forbid assignment. More...
 
void doExec ()
 Thread-safe execution of the initialization function (if still needed). More...
 

Private Attributes

Mutex itsMutex
 
InitFuncitsFunc
 
void * itsArg
 
volatile Bool itsDoExec
 

Detailed Description

Thread-safe initialization of global variables.

Intended use:

Public interface

Review Status

Reviewed By:
UNKNOWN
Date Reviewed:
before2004/08/25

Synopsis

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;

Example

// Declare static variables.
static volatile Bool needInit = True;
static Mutex mutex;
// Execute the code in a scope, so the destructor is called automatically.
{
CheckedMutexLock locker(mutex, needInit);
if (locker.doIt()) {
.. do the initialization
}
}

Definition at line 161 of file Mutex.h.

Member Typedef Documentation

§ InitFunc

typedef void casacore::MutexedInit::InitFunc(void *)

Define the initialization function to call.

Definition at line 165 of file Mutex.h.

Constructor & Destructor Documentation

§ MutexedInit() [1/2]

casacore::MutexedInit::MutexedInit ( InitFunc func,
void *  arg = 0,
Mutex::Type  type = Mutex::Auto 
)

Create the mutex and set that the initialization should be done.

§ MutexedInit() [2/2]

casacore::MutexedInit::MutexedInit ( const MutexedInit )
private

Forbid copy constructor.

Member Function Documentation

§ doExec()

void casacore::MutexedInit::doExec ( )
private

Thread-safe execution of the initialization function (if still needed).

§ exec()

void casacore::MutexedInit::exec ( )
inline

§ mutex()

Mutex& casacore::MutexedInit::mutex ( )
inline

Get the mutex (to make it possible to lock for other purposes).

Definition at line 175 of file Mutex.h.

References casacore::Mutex::itsMutex, and casacore::Mutex::operator=().

§ operator=()

MutexedInit& casacore::MutexedInit::operator= ( const MutexedInit )
private

Forbid assignment.

Member Data Documentation

§ itsArg

void* casacore::MutexedInit::itsArg
private

Definition at line 190 of file Mutex.h.

§ itsDoExec

volatile Bool casacore::MutexedInit::itsDoExec
private

Definition at line 191 of file Mutex.h.

§ itsFunc

InitFunc* casacore::MutexedInit::itsFunc
private

Definition at line 189 of file Mutex.h.

§ itsMutex

Mutex casacore::MutexedInit::itsMutex
private

Definition at line 188 of file Mutex.h.


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