casa
5.7.0-16
|
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... | |
CallOnce0 & | operator= (const CallOnce0 &) |
Forbid assignment. More... | |
Private Attributes | |
Bool | itsFlag |
Wrapper around std::call_once.
Public interface
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.
(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.
|
inline |
Definition at line 217 of file Mutex.h.
References itsFlag, and casacore::True.
|
private |
Forbid copy constructor.
|
inline |
Definition at line 224 of file Mutex.h.
References casacore::False, and itsFlag.
|
private |
Definition at line 245 of file Mutex.h.
Referenced by CallOnce0(), and operator()().