Merge pull request #5 from ichdream/dp-ich

Linux平台下单例模式多线程编程支持
master
Qiangguo Feng 2021-09-13 19:30:54 +08:00 committed by GitHub
commit d37087876b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 123 additions and 16 deletions

View File

@ -0,0 +1,29 @@
DIR_INC = ./
DIR_SRC = ./
DIR_OBJ = ./obj
DIR_BIN = ./bin
SRC = $(wildcard ${DIR_SRC}/*.cpp)
OBJ = $(patsubst %.cpp,${DIR_OBJ}/%.o,$(notdir ${SRC}))
TARGET = main
BIN_TARGET = ${DIR_BIN}/${TARGET}
CC = g++
CFLAGS = -g -Wall -I${DIR_INC} -pthread
${BIN_TARGET}:${OBJ}
mkdir -p $(DIR_BIN)
$(CC) $(CFLAGS) $(OBJ) -o $@
${DIR_OBJ}/%.o:${DIR_SRC}/%.cpp
mkdir -p $(DIR_OBJ)
$(CC) $(CFLAGS) -c $< -o $@
.PHONY:clean
clean:
rm -rf bin obj
rm -rf ./main

View File

@ -6,28 +6,45 @@
#include <mutex>
using namespace std;
class Singleton
class Singleton_Lazy
{
public:
static Singleton* getInstance(){
static Singleton_Lazy* getInstance(){
printf("This is Singleton Lazy mode...\n");
if (instance == NULL){
m_mutex.lock();
if (instance == NULL){
printf("创建新的实例\n");
instance = new Singleton();
printf("创建新的实例\n");
instance = new Singleton_Lazy();
}
m_mutex.unlock();
}
return instance;
}
private:
Singleton(){}
Singleton_Lazy(){}
static Singleton* instance;
static Singleton_Lazy* instance;
static std::mutex m_mutex;
};
Singleton* Singleton::instance = NULL;
std::mutex Singleton::m_mutex;
Singleton_Lazy* Singleton_Lazy::instance = NULL;
std::mutex Singleton_Lazy::m_mutex;
class Singleton_Hungry
{
public:
static Singleton_Hungry* getInstance()
{
printf("This Singleton Hungry mode...\n");
return instance;
}
private:
Singleton_Hungry() {}
static Singleton_Hungry* instance;
};
Singleton_Hungry* Singleton_Hungry::instance = new Singleton_Hungry;
#endif //__SINGLETON_H__

View File

@ -1,7 +1,7 @@
#include <iostream>
#include "Singleton.h"
/*单例模式简单实现*/
/*单例模式简单实现*/
/*
int main()
{
@ -13,11 +13,12 @@ int main()
}
*/
/*非线程安全 单例模式*/
#ifdef win32
/*非线程安全 单例模式*/
#include <process.h>
#include <Windows.h>
//多线程线程数目5
//多线程线程数目5
#define THREAD_NUM 5
unsigned int __stdcall CallSingleton(void *pPM)
@ -25,7 +26,7 @@ unsigned int __stdcall CallSingleton(void *pPM)
Singleton *s = Singleton::getInstance();
int nThreadNum = *(int *)pPM;
Sleep(50);
//printf("线程编号为%d\n", nThreadNum);
//printf("线程编号为%d\n", nThreadNum);
return 0;
}
@ -34,17 +35,77 @@ int main()
{
HANDLE handle[THREAD_NUM];
//线程编号
//线程编号
int threadNum = 0;
while (threadNum < THREAD_NUM)
{
handle[threadNum] = (HANDLE)_beginthreadex(NULL, 0, CallSingleton, &threadNum, 0, NULL);
//等子线程接收到参数时主线程可能改变了这个i的值
//等子线程接收到参数时主线程可能改变了这个i的值
threadNum++;
}
//保证子线程已全部运行结束
//保证子线程已全部运行结束
WaitForMultipleObjects(THREAD_NUM, handle, TRUE, INFINITE);
system("pause");
return 0;
}
/* for linux platform */
#else
#define THREAD_NUM 6
#include<pthread.h>
void* callSingleton_Lazy(void*)
{
Singleton_Lazy *s = Singleton_Lazy::getInstance();
pthread_t nThreadNum = pthread_self();
// sleep(50);
printf("线程编号为%lu\n", nThreadNum);
return 0;
}
void* callSingleton_Hungry(void*)
{
Singleton_Hungry *s = Singleton_Hungry::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], i;
void* status;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
for(i = 0; i < THREAD_NUM; i++)
{
if(i < THREAD_NUM / 2)
tids[i] = pthread_create(&threads_pool[i], NULL, callSingleton_Lazy, (void*)&i);
else
tids[i] = pthread_create(&threads_pool[i], NULL, callSingleton_Hungry, (void*)&i);
if(tids[i])
{
printf("Error: unable to create thread.\n");
exit(-1);
}
}
pthread_attr_destroy(&attr);
for(i = 0; i < THREAD_NUM; i++)
{
tids[i] = pthread_join(threads_pool[i], &status);
if(tids[i])
{
printf("Error: unable to join.\n");
exit(-1);
}
}
printf("main exiting.\n");
return 0;
}
#endif