diff --git a/6.Singleton/1.Picture/单例模式UML图.png b/6.Singleton/1.Picture/单例模式UML图.png new file mode 100644 index 0000000..df48d4e Binary files /dev/null and b/6.Singleton/1.Picture/单例模式UML图.png differ diff --git a/6.Singleton/2.Code/Singleton.h b/6.Singleton/2.Code/Singleton.h new file mode 100644 index 0000000..3ff8a6a --- /dev/null +++ b/6.Singleton/2.Code/Singleton.h @@ -0,0 +1,33 @@ +#ifndef __SINGLETON_H__ +#define __SINGLETON_H__ + +#include +#include +#include +using namespace std; + +class Singleton +{ +public: + static Singleton* getInstance(){ + if (instance == NULL){ + m_mutex.lock(); + if (instance == NULL){ + printf("µʵ\n"); + instance = new Singleton(); + } + m_mutex.unlock(); + } + return instance; + } +private: + Singleton(){} + + static Singleton* instance; + static std::mutex m_mutex; +}; + +Singleton* Singleton::instance = NULL; +std::mutex Singleton::m_mutex; + +#endif //__SINGLETON_H__ \ No newline at end of file diff --git a/6.Singleton/2.Code/main.cpp b/6.Singleton/2.Code/main.cpp new file mode 100644 index 0000000..bee839c --- /dev/null +++ b/6.Singleton/2.Code/main.cpp @@ -0,0 +1,50 @@ +#include +#include "Singleton.h" + +/*ģʽʵ*/ +/* +int main() +{ + Singleton *s1 = Singleton::getInstance(); + Singleton *s2 = Singleton::getInstance(); + + system("pause"); + return 0; +} +*/ + +/*̰߳ȫ ģʽ*/ +#include +#include + +//̣߳߳Ŀ5 +#define THREAD_NUM 5 + +unsigned int __stdcall CallSingleton(void *pPM) +{ + Singleton *s = Singleton::getInstance(); + int nThreadNum = *(int *)pPM; + Sleep(50); + //printf("̱߳Ϊ%d\n", nThreadNum); + return 0; +} + + +int main() +{ + HANDLE handle[THREAD_NUM]; + + //̱߳ + int threadNum = 0; + while (threadNum < THREAD_NUM) + { + handle[threadNum] = (HANDLE)_beginthreadex(NULL, 0, CallSingleton, &threadNum, 0, NULL); + //߳̽յʱܸ߳̿ıiֵ + threadNum++; + } + //֤߳ȫн + WaitForMultipleObjects(THREAD_NUM, handle, TRUE, INFINITE); + + system("pause"); + return 0; +} \ No newline at end of file diff --git a/6.SingletonPattern/2.Code/Singleton.h b/6.SingletonPattern/2.Code/Singleton.h new file mode 100644 index 0000000..3ff8a6a --- /dev/null +++ b/6.SingletonPattern/2.Code/Singleton.h @@ -0,0 +1,33 @@ +#ifndef __SINGLETON_H__ +#define __SINGLETON_H__ + +#include +#include +#include +using namespace std; + +class Singleton +{ +public: + static Singleton* getInstance(){ + if (instance == NULL){ + m_mutex.lock(); + if (instance == NULL){ + printf("µʵ\n"); + instance = new Singleton(); + } + m_mutex.unlock(); + } + return instance; + } +private: + Singleton(){} + + static Singleton* instance; + static std::mutex m_mutex; +}; + +Singleton* Singleton::instance = NULL; +std::mutex Singleton::m_mutex; + +#endif //__SINGLETON_H__ \ No newline at end of file diff --git a/6.SingletonPattern/2.Code/main.cpp b/6.SingletonPattern/2.Code/main.cpp new file mode 100644 index 0000000..bee839c --- /dev/null +++ b/6.SingletonPattern/2.Code/main.cpp @@ -0,0 +1,50 @@ +#include +#include "Singleton.h" + +/*ģʽʵ*/ +/* +int main() +{ + Singleton *s1 = Singleton::getInstance(); + Singleton *s2 = Singleton::getInstance(); + + system("pause"); + return 0; +} +*/ + +/*̰߳ȫ ģʽ*/ +#include +#include + +//̣߳߳Ŀ5 +#define THREAD_NUM 5 + +unsigned int __stdcall CallSingleton(void *pPM) +{ + Singleton *s = Singleton::getInstance(); + int nThreadNum = *(int *)pPM; + Sleep(50); + //printf("̱߳Ϊ%d\n", nThreadNum); + return 0; +} + + +int main() +{ + HANDLE handle[THREAD_NUM]; + + //̱߳ + int threadNum = 0; + while (threadNum < THREAD_NUM) + { + handle[threadNum] = (HANDLE)_beginthreadex(NULL, 0, CallSingleton, &threadNum, 0, NULL); + //߳̽յʱܸ߳̿ıiֵ + threadNum++; + } + //֤߳ȫн + WaitForMultipleObjects(THREAD_NUM, handle, TRUE, INFINITE); + + system("pause"); + return 0; +} \ No newline at end of file