17 Oct
2008
17 Oct
'08
2:56 a.m.
On Thursday 16 October 2008 18:42, Peter Dimov wrote:
The problem is that the default constructor may be invoked after the first call to Register::global, overwriting _global and resetting it to empty. The first subsequent call to global() would now reinitialize it.
You can avoid this by making _global a local static:
static RegisterP global() { static RegisterP _global( new Register ); return _global; }
Hmm, so I guess it is impossible to make a thread-safe singleton that avoids the static initialization order fiasco, unless your compiler guarantees thread-safe initialization of local statics. Even boost::call_once would require the once_flag to be statically initialized before the call_once call happens.