懒汉 多线程 单例模式
HANDLE g_handle = ::CreateMutex( NULL , FALSE , NULL );
class Singleton
{
public:
~Singleton()
{
cout << "析构函数\n" << endl;
}
static Singleton* getInstance()
{
if( NULL == m_pInstance )
{
WaitForSingleObject( g_handle , INFINITE );
if( NULL == m_pInstance )
{
m_pInstance = new Singleton;
}
ReleaseMutex( g_handle );
}
return m_pInstance;
}
private:
static Singleton* m_pInstance ;
};
Singleton* Singleton::m_pInstance = NULL ;
void main()
{
Singleton* p = Singleton::getInstance();
delete p ;
system("pause");
}