單例
單例模式的一種實(shí)現(xiàn)(《Effective C++》)
class S
{
public:
static S& getInstance()
{
static S instance;
return instance;
}
private:
S() {};
S(S const&) = delete;
void operator=(S const&) = delete;
};
此處是通過C++11新的語義來保證線程的安全性宏浩,具體由編譯器去處理绽榛,編譯器可能使用DCLP的方式或者其他的方式完成处面。
The C++11 standard §6.7.4:
If control enters the declaration concurrently while the variable is being initialized, the concurrent execution shall wait for completion of the initialization.