casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Public Types | Public Member Functions | Private Member Functions | Private Attributes
casa::MutexedInit Class Reference

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

#include <Mutex.h>

List of all members.

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.
Mutexmutex ()
 Get the mutex (to make it possible to lock for other purposes).

Private Member Functions

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

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

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

Define the initialization function to call.

Definition at line 165 of file Mutex.h.


Constructor & Destructor Documentation

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

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

Forbid copy constructor.


Member Function Documentation

void casa::MutexedInit::doExec ( ) [private]

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

Referenced by exec().

void casa::MutexedInit::exec ( ) [inline]

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

Definition at line 175 of file Mutex.h.

References itsMutex.

MutexedInit& casa::MutexedInit::operator= ( const MutexedInit ) [private]

Forbid assignment.


Member Data Documentation

void* casa::MutexedInit::itsArg [private]

Definition at line 190 of file Mutex.h.

volatile Bool casa::MutexedInit::itsDoExec [private]

Definition at line 191 of file Mutex.h.

Referenced by exec().

Definition at line 189 of file Mutex.h.

Definition at line 188 of file Mutex.h.

Referenced by mutex().


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