From 2c293e434b27df46f29946a0f8ce55f3ad2036a9 Mon Sep 17 00:00:00 2001 From: ichdream Date: Fri, 10 Sep 2021 15:22:43 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=AF=B9Linux=E5=B9=B3?= =?UTF-8?q?=E5=8F=B0=E5=A4=9A=E7=BA=BF=E7=A8=8B=E5=8D=95=E4=BE=8B=E6=A8=A1?= =?UTF-8?q?=E5=BC=8F=E8=B0=83=E7=94=A8=E7=9A=84=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 06.Singleton/2.Code/Singleton/Singleton.h | 2 +- 06.Singleton/2.Code/Singleton/main.cpp | 43 ++++++++++++++++++----- 2 files changed, 36 insertions(+), 9 deletions(-) diff --git a/06.Singleton/2.Code/Singleton/Singleton.h b/06.Singleton/2.Code/Singleton/Singleton.h index 3ff8a6a..ca8d9a6 100644 --- a/06.Singleton/2.Code/Singleton/Singleton.h +++ b/06.Singleton/2.Code/Singleton/Singleton.h @@ -13,7 +13,7 @@ public: if (instance == NULL){ m_mutex.lock(); if (instance == NULL){ - printf("创建新的实例\n"); + printf("鍒涘缓鏂扮殑瀹炰緥\n"); instance = new Singleton(); } m_mutex.unlock(); diff --git a/06.Singleton/2.Code/Singleton/main.cpp b/06.Singleton/2.Code/Singleton/main.cpp index bee839c..823a31b 100644 --- a/06.Singleton/2.Code/Singleton/main.cpp +++ b/06.Singleton/2.Code/Singleton/main.cpp @@ -1,7 +1,7 @@ #include #include "Singleton.h" -/*单例模式简单实现*/ +/*鍗曚緥妯″紡绠鍗曞疄鐜*/ /* int main() { @@ -13,11 +13,12 @@ int main() } */ -/*非线程安全 单例模式*/ +#ifdef win32 +/*闈炵嚎绋嬪畨鍏 鍗曚緥妯″紡*/ #include #include -//多线程,线程数目: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,43 @@ 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; -} \ No newline at end of file +} +/* for linux platform */ +#else +#define THREAD_NUM 5 +#include +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 \ No newline at end of file From 19ca5f026c2eab97396732cdf21b352ba39ac890 Mon Sep 17 00:00:00 2001 From: ichdream Date: Fri, 10 Sep 2021 15:23:29 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E7=BB=9F=E4=B8=80=E7=94=A8Makefile?= =?UTF-8?q?=E7=AE=A1=E7=90=86=E7=BC=96=E8=AF=91=E8=BF=87=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 06.Singleton/2.Code/Singleton/Makefile | 29 ++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 06.Singleton/2.Code/Singleton/Makefile diff --git a/06.Singleton/2.Code/Singleton/Makefile b/06.Singleton/2.Code/Singleton/Makefile new file mode 100644 index 0000000..e7b65b7 --- /dev/null +++ b/06.Singleton/2.Code/Singleton/Makefile @@ -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 \ No newline at end of file From e0c7e3db2552ec00b48fcf552d2ecc5550c0aed8 Mon Sep 17 00:00:00 2001 From: ichdream Date: Fri, 10 Sep 2021 15:55:18 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=8D=95=E4=BE=8B?= =?UTF-8?q?=E7=9A=84=E9=A5=BF=E6=B1=89=E6=A8=A1=E5=BC=8F=E6=94=AF=E6=8C=81?= =?UTF-8?q?=EF=BC=8C=E9=A5=BF=E6=B1=89=E5=8D=95=E4=BE=8B=E6=A8=A1=E5=BC=8F?= =?UTF-8?q?=E6=9C=AC=E8=BA=AB=E6=98=AF=E5=A4=9A=E7=BA=BF=E7=A8=8B=E5=AE=89?= =?UTF-8?q?=E5=85=A8=E7=9A=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 06.Singleton/2.Code/Singleton/Singleton.h | 31 ++++++++++++++++++----- 06.Singleton/2.Code/Singleton/main.cpp | 24 +++++++++++++++--- 2 files changed, 45 insertions(+), 10 deletions(-) diff --git a/06.Singleton/2.Code/Singleton/Singleton.h b/06.Singleton/2.Code/Singleton/Singleton.h index ca8d9a6..4589428 100644 --- a/06.Singleton/2.Code/Singleton/Singleton.h +++ b/06.Singleton/2.Code/Singleton/Singleton.h @@ -6,28 +6,45 @@ #include 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(); + 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__ \ No newline at end of file diff --git a/06.Singleton/2.Code/Singleton/main.cpp b/06.Singleton/2.Code/Singleton/main.cpp index 823a31b..d316883 100644 --- a/06.Singleton/2.Code/Singleton/main.cpp +++ b/06.Singleton/2.Code/Singleton/main.cpp @@ -53,9 +53,18 @@ int main() #else #define THREAD_NUM 5 #include -void* callSingleton(void *pPM) +void* callSingleton_Lazy(void *pPM) { - Singleton *s = Singleton::getInstance(); + Singleton_Lazy *s = Singleton_Lazy::getInstance(); + pthread_t nThreadNum = pthread_self(); + // sleep(50); + printf("绾跨▼缂栧彿涓%ld\n", nThreadNum); + return 0; +} + +void* callSingleton_Hungry(void *pPM) +{ + Singleton_Hungry *s = Singleton_Hungry::getInstance(); pthread_t nThreadNum = pthread_self(); // sleep(50); printf("绾跨▼缂栧彿涓%ld\n", nThreadNum); @@ -66,11 +75,20 @@ int main() { pthread_t threads_pool[THREAD_NUM]; int tids[THREAD_NUM]; + printf("Singleton Lazy mode:\n"); for(int i = 0; i < THREAD_NUM; i++) { - tids[i] = pthread_create(&threads_pool[i], NULL, callSingleton, NULL); + tids[i] = pthread_create(&threads_pool[i], NULL, callSingleton_Lazy, NULL); pthread_join(threads_pool[i], (void**)&tids[i]); } + + printf("Singleton Hungry mode:\n"); + for(int i = 0; i < THREAD_NUM; i++) + { + tids[i] = pthread_create(&threads_pool[i], NULL, callSingleton_Hungry, NULL); + pthread_join(threads_pool[i], (void**)&tids[i]); + } + return 0; } From 0239628ce4d01c04e155db11e68dfe79a1921a40 Mon Sep 17 00:00:00 2001 From: ichdream Date: Fri, 10 Sep 2021 16:37:10 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E5=8E=9F=E6=9D=A5=E7=9A=84main.cpp?= =?UTF-8?q?=E5=A4=9A=E7=BA=BF=E7=A8=8B=E6=89=A7=E8=A1=8C=E7=9A=84=E6=97=B6?= =?UTF-8?q?=E5=80=99=E6=9C=89bug=EF=BC=8C=E5=AE=9E=E9=99=85=E6=89=A7?= =?UTF-8?q?=E8=A1=8C=E7=9A=84=E6=98=AF=E5=8D=95=E7=BA=BF=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 06.Singleton/2.Code/Singleton/main.cpp | 44 ++++++++++++++++++-------- 1 file changed, 30 insertions(+), 14 deletions(-) diff --git a/06.Singleton/2.Code/Singleton/main.cpp b/06.Singleton/2.Code/Singleton/main.cpp index d316883..d20ee4a 100644 --- a/06.Singleton/2.Code/Singleton/main.cpp +++ b/06.Singleton/2.Code/Singleton/main.cpp @@ -51,18 +51,18 @@ int main() } /* for linux platform */ #else -#define THREAD_NUM 5 +#define THREAD_NUM 6 #include -void* callSingleton_Lazy(void *pPM) +void* callSingleton_Lazy(void*) { Singleton_Lazy *s = Singleton_Lazy::getInstance(); pthread_t nThreadNum = pthread_self(); // sleep(50); - printf("绾跨▼缂栧彿涓%ld\n", nThreadNum); + printf("绾跨▼缂栧彿涓%lu\n", nThreadNum); return 0; } -void* callSingleton_Hungry(void *pPM) +void* callSingleton_Hungry(void*) { Singleton_Hungry *s = Singleton_Hungry::getInstance(); pthread_t nThreadNum = pthread_self(); @@ -74,21 +74,37 @@ void* callSingleton_Hungry(void *pPM) int main() { pthread_t threads_pool[THREAD_NUM]; - int tids[THREAD_NUM]; - printf("Singleton Lazy mode:\n"); - for(int i = 0; i < THREAD_NUM; i++) + 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++) { - tids[i] = pthread_create(&threads_pool[i], NULL, callSingleton_Lazy, NULL); - pthread_join(threads_pool[i], (void**)&tids[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); + } } - printf("Singleton Hungry mode:\n"); - for(int i = 0; i < THREAD_NUM; i++) + pthread_attr_destroy(&attr); + for(i = 0; i < THREAD_NUM; i++) { - tids[i] = pthread_create(&threads_pool[i], NULL, callSingleton_Hungry, NULL); - pthread_join(threads_pool[i], (void**)&tids[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; }