DesignPattern/06.Singleton/2.Code/Singleton/main.cpp

50 lines
895 B
C++
Raw Normal View History

2019-10-20 10:48:24 +00:00
#include <iostream>
#include "Singleton.h"
/*<2A><><EFBFBD><EFBFBD>ģʽ<C4A3><CABD><EFBFBD><EFBFBD>ʵ<EFBFBD><CAB5>*/
/*
int main()
{
Singleton *s1 = Singleton::getInstance();
Singleton *s2 = Singleton::getInstance();
system("pause");
return 0;
}
*/
/*<2A><><EFBFBD>̰߳<DFB3>ȫ <20><><EFBFBD><EFBFBD>ģʽ*/
#include <process.h>
#include <Windows.h>
//<2F><><EFBFBD>̣߳<DFB3><CCA3>߳<EFBFBD><DFB3><EFBFBD>Ŀ<EFBFBD><C4BF>5
#define THREAD_NUM 5
unsigned int __stdcall CallSingleton(void *pPM)
{
Singleton *s = Singleton::getInstance();
int nThreadNum = *(int *)pPM;
Sleep(50);
//printf("<22>̱߳<DFB3><CCB1><EFBFBD>Ϊ%d\n", nThreadNum);
return 0;
}
int main()
{
HANDLE handle[THREAD_NUM];
//<2F>̱߳<DFB3><CCB1><EFBFBD>
int threadNum = 0;
while (threadNum < THREAD_NUM)
{
handle[threadNum] = (HANDLE)_beginthreadex(NULL, 0, CallSingleton, &threadNum, 0, NULL);
//<2F><><EFBFBD><EFBFBD><EFBFBD>߳̽<DFB3><CCBD>յ<EFBFBD><D5B5><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD>߳̿<DFB3><CCBF>ܸı<DCB8><C4B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>i<EFBFBD><69>ֵ
threadNum++;
}
//<2F><>֤<EFBFBD><D6A4><EFBFBD>߳<EFBFBD><DFB3><EFBFBD>ȫ<EFBFBD><C8AB><EFBFBD><EFBFBD><EFBFBD>н<EFBFBD><D0BD><EFBFBD>
WaitForMultipleObjects(THREAD_NUM, handle, TRUE, INFINITE);
system("pause");
return 0;
}