casa  5.7.0-16
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Public Member Functions | Private Member Functions | Private Attributes | List of all members
casacore::CallOnce0 Class Reference

Wrapper around std::call_once. More...

#include <Mutex.h>

Public Member Functions

 CallOnce0 ()
 
void operator() (void(*fn)())
 

Private Member Functions

 CallOnce0 (const CallOnce0 &)
 Forbid copy constructor. More...
 
CallOnce0operator= (const CallOnce0 &)
 Forbid assignment. More...
 

Private Attributes

Bool itsFlag
 

Detailed Description

Wrapper around std::call_once.

Intended use:

Public interface

Review Status

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

Synopsis

Ease correct lazy initialization of static data in the easy cases.
Often data needs to be initialized once and accessed many times. To do this in a thread-safe way, a mutex is expensive, and basic double-checked locking is not fully thread-safe. The C++ function std::call_once offers the required functionality. The classes CallOnce and CallOnce0 are a little wrappers around this function to call it with 0 or 1 argument.

Example

(Note that this simple case can also be solved with C++11 thread-safe initialization of function statics (can be slightly more efficient).) all under namespace casacore: .h: class A { static std::vector<int> theirSharedInts; static CallOnce theirInitOnce; void initX();

public: int getX(size_t idx); }; .cc: vector<int> A::theirSharedInts(1); CallOnce A::theirInitOnce;

void A::initX(int val) { theirSharedInts[0] = val; }

int A::getX(size_t idx) { theirInitOnce(initX, 42); return theirSharedInts.at(idx); }

CallOnce0: func has 0 args.

Definition at line 214 of file Mutex.h.

Constructor & Destructor Documentation

casacore::CallOnce0::CallOnce0 ( )
inline

Definition at line 217 of file Mutex.h.

References itsFlag, and casacore::True.

casacore::CallOnce0::CallOnce0 ( const CallOnce0 )
private

Forbid copy constructor.

Member Function Documentation

void casacore::CallOnce0::operator() ( void(*)()  fn)
inline

Definition at line 224 of file Mutex.h.

References casacore::False, and itsFlag.

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

Forbid assignment.

Member Data Documentation

Bool casacore::CallOnce0::itsFlag
private

Definition at line 245 of file Mutex.h.

Referenced by CallOnce0(), and operator()().


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