DesignPattern/06.Singleton/2.Code/Singleton/Singleton.h

33 lines
553 B
C++

#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__