From e0c7e3db2552ec00b48fcf552d2ecc5550c0aed8 Mon Sep 17 00:00:00 2001 From: ichdream Date: Fri, 10 Sep 2021 15:55:18 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=8D=95=E4=BE=8B=E7=9A=84?= =?UTF-8?q?=E9=A5=BF=E6=B1=89=E6=A8=A1=E5=BC=8F=E6=94=AF=E6=8C=81=EF=BC=8C?= =?UTF-8?q?=E9=A5=BF=E6=B1=89=E5=8D=95=E4=BE=8B=E6=A8=A1=E5=BC=8F=E6=9C=AC?= =?UTF-8?q?=E8=BA=AB=E6=98=AF=E5=A4=9A=E7=BA=BF=E7=A8=8B=E5=AE=89=E5=85=A8?= =?UTF-8?q?=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; }