????DCL
????DCL??double-checked locking?????????????д???У???ε???getInstance????????????????????heavy contention???????ú???????????????????????????????????????DCLд??????????????check?????????check??????????????е????check??
template<typename T>
class Singleton
{
public:
static T& getInstance()
{
if(!value_)
{
MutexGuard guard(mutex_);
if (!value_)
{
value_ = new T();
}
}
return *value_;
}
private:
Singleton();
~Singleton();
static T*     value_;
static Mutex  mutex_;
};
template<typename T>
T* Singleton<T>::value_ = NULL;
template<typename T>
Mutex Singleton<T>::mutex_;
?????????????????????????????????????????????????????????Ч??????????????????????????????????????д?????????DCL?е????????????????????д???????????????г????
??????????????????????????????12??value_ = new T?????????????
?????????????T????????????????檔
????????????洦????T????????
???????????????????????value_
??????????????????ü?????????1??2??3?????????д???????????????????????????????1????У???????2??3?????????????????????????????A?????getInstance??????12?е??????1??3??2???????У????????????????3???????????л????????????????????????B??????????check?????????????????????B?е???getInstance???????????????check????????????????????????value_???????????????????*value_?????к??????T?????????????????A????в???3???????У???????B????????getInstance??????????б???????????????????????Щ????debug??????????
????volatile???????????????????????????
???????????????????£?2????????????????A??B??????????????????check???????????????????????????б???????????????
????????DCL?????????????????????ο?Scott Meyer??paper????C++ and the Perils of Double-Checked Locking??
???????????μ?C++11?У????????????????????μ?C++11?漲???μ????????????????????3??????????????????л???????????????????“?????”????????DCL????????????????????C++11????и?????????Singletonд?????????????????????
?????????μ?C++11?????????????ο???C++11?????FAQ?????????C++11FAQ??Memory Model??C++ Data-Dependency Ordering: Atomics and Memory Model
?????????????????????????????????C++11????汾?£????DCL???????????????????е???????????2??3?????????memory barrier?????CPU??е??????1??2??3??????????С?(??????@shines77??????????????????????????RCU????????read-copy-update)
????static T& getInstance()
????{
????if(!value_)
????{
????MutexGuard guard(mutex_);
????if (!value_)
????{
????T* p = static_cast<T*>(operator new(sizeof(T)));
????new (p) T();
????// insert some memory barier
????value_ = p;  // RCU method
????}
????}
????return *value_;
????}
?????????????????????????????value_ = new T()?????????????????????????? ??????????????some memory barrier??
????T* p = static_cast<T*>(operator new(sizeof(T)));
????new (p) T();
????????????????????????Out-of-order execution????????У??????????????????CPU???????????????????????????е???????磬????????Intel CPU ????6????е????????????????????????????????????????????е??????????Щ???????????Щ??е?????е??????????????е???С?????????? CPU ???????????????????????????????
?????????????CPU???????????????????CPU???????????????????????????????????????????“??????”???????е???д???????????????????????????е???????????????????д?????????????????????????????????????????????????CPU?????е?????????????????????????μ??????????????????CPU??????????????????“?????”???
???????????????????????memory barrier??????????????CPU???????????????С????????????汾??getInstance?????У??????10????memory barrier??????CPU??е?9??10??11??“???”??е???????CPU?????????????????????????????е?9?С?????????е?11?У??????????????????????retirement unit????????н??????ü????????????????????9??10??11?????μ????????????A??????????11?У?CPU????A????????е?9?е???B????????????????????????????????????????A??????
????memory barreir???????????????????????????????????????£????ο?????Mutex And Memory Visibility??
???????store buffer??
??????????memory barreir????????????ɡ?
????????memory barreir??????????memory barreir????С?
??????????memory barreir??????????????????????????????????δ????д???????????μ????檔????????????????????????????????????????????????????????????????????????????????????????Щ???????????????????????memory barrier???????
??????????μ???memory barrier????unix???????????????barrier()??????????????????ASM??????mfence??????barrier??????????????????
????????memory barreir????ο?????Memory Barriers/Fences??
????Meyers Singleton
????Scott Meyer???Effective C++???????????????singletonд??
????template<typename T>
????class Singleton
????{
????public:
????static T& getInstance()
????{
????static T value;
????return value;
????}
????private:
????Singleton();
????~Singleton();
????};
????????????
??????????£??????
????C++11??????汾????C++14?????????£??????
????C++11?????????£???????????
?????????????C++11???????в???й漲local static?????????????????????????????local static??????????????????????check???ο??????????C++????????????????getInstance??????????????д?????????
????bool initialized = false;
????char value[sizeof(T)];
????T& getInstance()
????{
????if (!initialized)
????{
????initialized = true;
????new (value) T();
????}
????return *(reinterpret_cast<T*>(value));
????}
??????????????????????????