我在C++中看到了經典的單例模式類。我的理解是這個實現是線程安全的。然後我讀,如果這個類包含在2個DLL和兩者都在1個加載應用程序,你會得到靜態變量的2份因此S級的2個實例,因此它不完全是線程安全的。C++中的經典單例模式
那麼解決方案仍在使用互斥鎖? (我知道這是在C#中常見的做法,在詳細:http://csharpindepth.com/Articles/General/Singleton.aspx
class S
{
public:
static S& getInstance()
{
static S instance;
return instance;
}
private:
S();
S(S const&); // Don't Implement
void operator=(S const&); // Don't implement
};
參見:http://stackoverflow.com/questions/1008019/c-singleton-design-pattern – MatthewD