template <class T>
class ISingleton
{
public:
static T& Instance()
{
static void* volatile pInstance = NULL;
static void* volatile creating = NULL;
if(pInstance)
return pInstance;
if(InterlockedCompareExchangePointer(&creating, (void*)1, 0) == 0)
{
static T instance;
InterlockedExchangePointer(&pInstance, &instance);
}else
{
if(!pInstance)
Sleep(1);
}
return pInstance;
}
private:
ISingleton();
ISingleton(const ISingleton&);
ISingleton& operator=(const ISingleton&);
};原文:http://blog.csdn.net/x313695373/article/details/24192053