??????????????????ο? C++???????????????????????????μ?????????????л???????????????£??????????????????????????????????С??
????????????????????????????????????????????????????????????????????????鹲?????????????÷?Χ???????????????????????????????????PC???????????
?????????????????????????????????????????????й???????????????C++????????????????Win7 64λ??VS2010??????????
????????ο??????е????????????о?????????????????????CSingleton?????????????
????1. ?????????????????CSingleton g_instance???????????????????????????????????????????????????CSingleton?????????
????2. ?????????????????????????е?????????????static CSingleton1 *m_pInstance;?????????????????????????????????????????????????
????3. ?????????????????????????е??????????static CSingleton1 m_pInstance;
????????????????????????????
????1. ?????????????? ??????????????????????????????????????????????????????
????2. ????????????????????main??????????????????????????????????????????????????????????????? CSingleton1* CSingleton::m_pInstance = NULL??????????????????????????????????????????????????????????д????
????4. ??????????????main???????????CRT???????????У????????????????????????????????????????????????迼?????????????main???????????????????С?

????3. ?????????????????????????????????????????????????????????????????????????????::
?????????????????????????????????????
?????????????????????????????????????????????????г??????????????????????????????????????????????????????????????????
class CSingleton
{
private:
CSingleton()   //??????????е?
{
}
CSingleton(const CSingleton &);
CSingleton & operator = (const CSingleton &);
public:
static CSingleton & GetInstance()
{
static CSingleton instance;   //??????????
return instance;
}
};
?????????????????????????????з?????????????????????????ο?C++???????????е?????????????????????????????й?????????????????д?????????????????
// singlethon.cpp : ??????????ó????????
#include <iostream>
#include <Windows.h>
using namespace std;
//???????CRT ???й????????
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
//????????????
class Lock
{
private:
CRITICAL_SECTION  m_cs; // ????????
Lock(){};
Lock(const Lock&){};
Lock& operator=(const Lock&){};
public:
Lock(CRITICAL_SECTION cs):m_cs(cs)
{
InitializeCriticalSection(&m_cs);
}
void StartLock()
{
EnterCriticalSection(&m_cs);
}
void StopLock()
{
// ???????
//LeaveCriticalSection(&m_cs);
}
~Lock()
{
// ??????? ???????? ò??????
LeaveCriticalSection(&m_cs);
// ??????????????????????????????
DeleteCriticalSection(&m_cs);
}
};
//???????ο???????????????
class Singlethon
{
//??????????Singlethon???????????????????
class CGarbo    //??????????????????У????Singlethon?????
{
public:
CGarbo()
{
cout << "constructor CGarbo"<<endl;
}
~CGarbo()
{
if(Singlethon::m_pInstance)
{
cout << "execute CGarbo destructor function"<<endl;
delete Singlethon::m_pInstance;
}
}
};
private:
Singlethon(){cout << "constructor Singlethon "<<endl;};    //?????????? Singlethon single;
Singlethon(const Singlethon&){};                        //?????????? Singlethon single2(*(Singlethon::GetInstance()));
//      ????   Singlethon single2 = (*(Singlethon::GetInstance()));
Singlethon& operator=(const Singlethon&){};        //??????????     Singlethon single;single = *(Singlethon::GetInstance());
static Singlethon* m_pInstance;
static CGarbo Garbo;        //???????????????????????????????????????????
static CRITICAL_SECTION cs;
public:
static Singlethon* GetInstance();
~Singlethon()
{
cout << "execute Singlethon destructor function"<<endl;
//?????????????????????CGarbox??????????????????????????У?????????????????? delete Singlethon::m_pInstance;
//?????Singlethon?????????????????????????ε??? delete Singlethon::m_pInstance??????????????
//???????????????
#if 0
if(Singlethon::m_pInstance)
delete Singlethon::m_pInstance;
Singlethon::m_pInstance = NULL;
#endif
}
};
//???????仰????????
Singlethon::CGarbo Singlethon::Garbo;
Singlethon* Singlethon::m_pInstance = NULL;
Singlethon* Singlethon::GetInstance()
{
if (NULL == m_pInstance)
{
Lock lock(cs);
lock.StartLock();
if (NULL == m_pInstance)
{
m_pInstance = new Singlethon;
}
lock.StopLock();
}    //???????????????????????????????lock???????????????lock??????????????
return m_pInstance;
}
int main()
{
//????CRT???й??????
_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
Singlethon* p1 = Singlethon::GetInstance();
Singlethon* p2 = Singlethon::GetInstance();
if ( p1 == p2)
{
cout << "????????????!!"<<endl;
}
else
{
cout << "?????????????!!"<<endl;
}
return 0;
}