添加对Linux平台多线程单例模式调用的支持
parent
92a17eed14
commit
2c293e434b
|
@ -13,7 +13,7 @@ public:
|
||||||
if (instance == NULL){
|
if (instance == NULL){
|
||||||
m_mutex.lock();
|
m_mutex.lock();
|
||||||
if (instance == NULL){
|
if (instance == NULL){
|
||||||
printf("创建新的实例\n");
|
printf("创建新的实例\n");
|
||||||
instance = new Singleton();
|
instance = new Singleton();
|
||||||
}
|
}
|
||||||
m_mutex.unlock();
|
m_mutex.unlock();
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include "Singleton.h"
|
#include "Singleton.h"
|
||||||
|
|
||||||
/*单例模式简单实现*/
|
/*单例模式简单实现*/
|
||||||
/*
|
/*
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
|
@ -13,11 +13,12 @@ int main()
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/*非线程安全 单例模式*/
|
#ifdef win32
|
||||||
|
/*非线程安全 单例模式*/
|
||||||
#include <process.h>
|
#include <process.h>
|
||||||
#include <Windows.h>
|
#include <Windows.h>
|
||||||
|
|
||||||
//多线程,线程数目:5
|
//多线程,线程数目:5
|
||||||
#define THREAD_NUM 5
|
#define THREAD_NUM 5
|
||||||
|
|
||||||
unsigned int __stdcall CallSingleton(void *pPM)
|
unsigned int __stdcall CallSingleton(void *pPM)
|
||||||
|
@ -25,7 +26,7 @@ unsigned int __stdcall CallSingleton(void *pPM)
|
||||||
Singleton *s = Singleton::getInstance();
|
Singleton *s = Singleton::getInstance();
|
||||||
int nThreadNum = *(int *)pPM;
|
int nThreadNum = *(int *)pPM;
|
||||||
Sleep(50);
|
Sleep(50);
|
||||||
//printf("线程编号为%d\n", nThreadNum);
|
//printf("线程编号为%d\n", nThreadNum);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -34,17 +35,43 @@ int main()
|
||||||
{
|
{
|
||||||
HANDLE handle[THREAD_NUM];
|
HANDLE handle[THREAD_NUM];
|
||||||
|
|
||||||
//线程编号
|
//线程编号
|
||||||
int threadNum = 0;
|
int threadNum = 0;
|
||||||
while (threadNum < THREAD_NUM)
|
while (threadNum < THREAD_NUM)
|
||||||
{
|
{
|
||||||
handle[threadNum] = (HANDLE)_beginthreadex(NULL, 0, CallSingleton, &threadNum, 0, NULL);
|
handle[threadNum] = (HANDLE)_beginthreadex(NULL, 0, CallSingleton, &threadNum, 0, NULL);
|
||||||
//等子线程接收到参数时主线程可能改变了这个i的值
|
//等子线程接收到参数时主线程可能改变了这个i的值
|
||||||
threadNum++;
|
threadNum++;
|
||||||
}
|
}
|
||||||
//保证子线程已全部运行结束
|
//保证子线程已全部运行结束
|
||||||
WaitForMultipleObjects(THREAD_NUM, handle, TRUE, INFINITE);
|
WaitForMultipleObjects(THREAD_NUM, handle, TRUE, INFINITE);
|
||||||
|
|
||||||
system("pause");
|
system("pause");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
/* for linux platform */
|
||||||
|
#else
|
||||||
|
#define THREAD_NUM 5
|
||||||
|
#include<pthread.h>
|
||||||
|
void* callSingleton(void *pPM)
|
||||||
|
{
|
||||||
|
Singleton *s = Singleton::getInstance();
|
||||||
|
pthread_t nThreadNum = pthread_self();
|
||||||
|
// sleep(50);
|
||||||
|
printf("线程编号为%ld\n", nThreadNum);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
pthread_t threads_pool[THREAD_NUM];
|
||||||
|
int tids[THREAD_NUM];
|
||||||
|
for(int i = 0; i < THREAD_NUM; i++)
|
||||||
|
{
|
||||||
|
tids[i] = pthread_create(&threads_pool[i], NULL, callSingleton, NULL);
|
||||||
|
pthread_join(threads_pool[i], (void**)&tids[i]);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
Loading…
Reference in New Issue