??????????C++11???????????????????μ?C++????漲???????????????????????????????????????????ó????????????????????
??????C++11 standard?е?§6.7 [stmt.dcl] p4??
????If control enters the declaration concurrently while the variable is being initialized?? the concurrent execution shall wait for completion of the initialization.
??????stackoverflow?е?Is Meyers implementation of Singleton pattern thread safe????????????????????
??????????Щ????????C++11????汾???????????????g++????g++4.0?????meyers singleton???????????????C++11????????????????????????????????????
????Atomic Singleton
??????C++11????汾?£????????????????????Singleton??????????????????????????atomic operation??????????????Atomic???????????????atomic operation??
template<typename T>
class Singleton
{
public:
static T& getInstance()
{
while (true)
{
if (ready_.get())
{
return *value_;
}
else
{
if (initializing_.getAndSet(true))
{
// another thread is initializing?? waiting in circulation
}
else
{
value_ = new T();
ready_.set(true);
return *value_;
}
}
}
}
private:
Singleton();
~Singleton();
static Atomic<bool>  ready_;
static Atomic<bool>  initializing_;
static T*            value_;
};
template<typename T>
Atomic<int> Singleton<T>::ready_(false);
template<typename T>
Atomic<int> Singleton<T>::initializing_(false);
template<typename T>
T* Singleton<T>::value_ = NULL;
?????????????????д?????????·?????????????????????
??????????????????
??????????й?????????????????????????
??????????й????????????κ?????????????
????pthread_once
???????????unix??????????????atomic operation?????????C++11??????£??????????pthread_once?????Singleton??
????pthread_once??????
????int pthread_once(pthread_once_t *once_control?? void (*init_routine)(void))
????APUE?ж???pthread_once??????????
??????????????????pthread_once?????????????????init_routine?????????Σ?????????ε???pthread_once???
?????????????????????????Singleton??
????template<typename T>
????class Singleton : Nocopyable
????{
????public:
????static T& getInstance()
????{
????threads::pthread_once(&once_control_?? init);
????return *value_;
????}
????private:
????static void init()
????{
????value_ = new T();
????}
????Singleton();
????~Singleton();
????static pthread_once_t  once_control_;
????static T*              value_;
????};
????template<typename T>
????pthread_once_t Singleton<T>::once_control_ = PTHREAD_ONCE_INIT;
????template<typename T>
????T* Singleton<T>::value_ = NULL;
????????????????????????????????init???????????glibc????atexit??????????????????????????????????????????????????????????
????static object
???????????????????????????????????????????C++11????????????atomic operation????????????????atomic????????????????????????????????????????static object??????????е??
template<typename T>
class Singleton
{
public:
static T& getInstance()
{
return *value_;
}
private:
Singleton();
~Singleton();
class Helper
{
public:
Helper()
{
Singleton<T>::value_ = new T();
}
~Helper()
{
delete value_;
value_ = NULL;
}
};
friend class Helper;
static T*      value_;
static Helper  helper_;
};
template<typename T>
T* Singleton<T>::value_ = NULL;
template<typename T>
typename Singleton<T>::Helper Singleton<T>::helper_;
?????????main????Singleton?????????????????????main???????????????й?????????????????д?????????????????main?????????????getInstance?????C++????????????????main????????????????ɡ?
?????????????????helper??????????value_?????????????helper_??????????????????б????????value_????????????????????????????????????value_“????”??????????????helper_??????????value_?????
????????????????????????value_?????????????helper_?????C++????????????????
????The storage for objects with static storage duration (basic.stc.static) shall be zero-initialized (dcl.init) before any other initialization takes place. Zero-initialization and initialization with a constant expression are collectively called static initialization; all other initialization is dynamic initialization. Objects of POD types (basic.types) with static storage duration initialized with constant expressions (expr.const) shall be initialized before any dynamic initialization takes place. Objects with static storage duration defined in namespace scope in the same translation unit and dynamically initialized shall be initialized in the order in which their definition appears in the translation unit.
????stackoverflow?е????????????????????????When are static C++ class members initialized?