#include <iostream>
#include <windows.h>
using namespace std;
class Single
{
public:
static Single & GetInstance() {
static Single _obj;
return _obj;
}
void Print()
{
cout << "hello world : " << endl;
}
protected:
Single()
{
}
~Single()
{
}
};
class Single1
{
public:
static Single1 * GetInstance() {
if (NULL == m_pSingle)
{
nCount++;
m_pSingle = new Single1;
}
return m_pSingle;
}
void Print()
{
cout << "Single1: hello world : " << nCount << endl;
}
protected:
Single1()
{
}
~Single1()
{
}
private:
static Single1* m_pSingle;
static int nCount;
};
class Single1 * Single1::m_pSingle = NULL;
int Single1::nCount = 0;
void WorkThread(void *pParam)
{
Sleep(rand()%100);
Single::GetInstance().Print();
}
void WorkThread1(void *pParam)
{
Sleep(rand()%100);
Single1::GetInstance()->Print();
}
int main(int argc, char *argv[])
{
Single::GetInstance().Print();
::CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)WorkThread, NULL, 0, 0);
::CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)WorkThread, NULL, 0, 0);
::CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)WorkThread, NULL, 0, 0);
::CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)WorkThread1, NULL, 0, 0);
::CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)WorkThread1, NULL, 0, 0);
::CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)WorkThread1, NULL, 0, 0);
getchar();
}
single model
最新推荐文章于 2020-05-27 21:16:12 发布