add Singleton Pattern

master
FengJungle 2019-10-20 18:48:24 +08:00
parent e539d4bbb4
commit 2e0045a1b2
7 changed files with 171 additions and 1 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.1 KiB

View File

@ -0,0 +1,33 @@
#ifndef __SINGLETON_H__
#define __SINGLETON_H__
#include <iostream>
#include <string.h>
#include <mutex>
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__

View File

@ -0,0 +1,50 @@
#include <iostream>
#include "Singleton.h"
/*单例模式简单实现*/
/*
int main()
{
Singleton *s1 = Singleton::getInstance();
Singleton *s2 = Singleton::getInstance();
system("pause");
return 0;
}
*/
/*非线程安全 单例模式*/
#include <process.h>
#include <Windows.h>
//多线程线程数目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;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.1 KiB

View File

@ -0,0 +1,33 @@
#ifndef __SINGLETON_H__
#define __SINGLETON_H__
#include <iostream>
#include <string.h>
#include <mutex>
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__

View File

@ -0,0 +1,50 @@
#include <iostream>
#include "Singleton.h"
/*单例模式简单实现*/
/*
int main()
{
Singleton *s1 = Singleton::getInstance();
Singleton *s2 = Singleton::getInstance();
system("pause");
return 0;
}
*/
/*非线程安全 单例模式*/
#include <process.h>
#include <Windows.h>
//多线程线程数目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;
}

View File

@ -31,4 +31,8 @@
08.设计模式(八)——原型模式
博客地址https://blog.csdn.net/sinat_21107433/article/details/102642682
博客地址https://blog.csdn.net/sinat_21107433/article/details/102642682
09.设计模式(九)——单例模式
博客地址https://blog.csdn.net/sinat_21107433/article/details/102649056