C++单例模式的实现
来源:互联网 发布:笔记本网卡mac地址修改 编辑:程序博客网 时间:2024/06/14 12:45
#ifndef SINGLETON_H
#define SINGLETON_H
#include <iostream>
#include <memory>
#include <mutex>
using namespace std;
namespace Common
{
template <class T>
class CSingleton
{
public:
static inline T* Instance();
private:
CSingleton(void) {}
~CSingleton(void) {}
CSingleton(const CSingleton&) {}
CSingleton & operator= (const CSingleton &) {}
static auto_ptr<T> _instance;
static std::mutex _mutex;
//static CQuickLock _rs;
};
template <class T>
auto_ptr<T> CSingleton<T>::_instance;
template <class T>
std::mutex CSingleton<T>::_mutex;
template <class T>
inline T* CSingleton<T>::Instance()
{
if (0 == _instance.get())
{
//上锁,防止多线程可能创建多个Singleton实例
std::lock_guard<std::mutex> lock(_mutex);
if (0 == _instance.get())
{
_instance.reset(new T);
}
}
return _instance.get();
}
}
/////////////////Singleton应用实例
class CMyLog
{
friend class auto_ptr<CMyLog>; //作为友元可以访问CMyLog的私有成员
friend class Common::CSingleton<CMyLog>; //作为友元可以访问CMyLog的私有成员
public:
void Log(char* pszStr)
{
cout << "Log msg: " << pszStr << endl;
}
public:
int a;
private:
int b;
CMyLog() {}; //不允许直接实例化
virtual ~CMyLog(void) {};
};
int main()
{
CMyLog* pLog = Common::CSingleton<CMyLog>::Instance();
pLog->Log("hello word");
pLog->a = 10;
CMyLog* pLog1 = Common::CSingleton<CMyLog>::Instance();
int b = pLog->a;
system("pause");
return 0;
}
#endif/*SINGLETON_H*/
#define SINGLETON_H
#include <iostream>
#include <memory>
#include <mutex>
using namespace std;
namespace Common
{
template <class T>
class CSingleton
{
public:
static inline T* Instance();
private:
CSingleton(void) {}
~CSingleton(void) {}
CSingleton(const CSingleton&) {}
CSingleton & operator= (const CSingleton &) {}
static auto_ptr<T> _instance;
static std::mutex _mutex;
//static CQuickLock _rs;
};
template <class T>
auto_ptr<T> CSingleton<T>::_instance;
template <class T>
std::mutex CSingleton<T>::_mutex;
template <class T>
inline T* CSingleton<T>::Instance()
{
if (0 == _instance.get())
{
//上锁,防止多线程可能创建多个Singleton实例
std::lock_guard<std::mutex> lock(_mutex);
if (0 == _instance.get())
{
_instance.reset(new T);
}
}
return _instance.get();
}
}
/////////////////Singleton应用实例
class CMyLog
{
friend class auto_ptr<CMyLog>; //作为友元可以访问CMyLog的私有成员
friend class Common::CSingleton<CMyLog>; //作为友元可以访问CMyLog的私有成员
public:
void Log(char* pszStr)
{
cout << "Log msg: " << pszStr << endl;
}
public:
int a;
private:
int b;
CMyLog() {}; //不允许直接实例化
virtual ~CMyLog(void) {};
};
int main()
{
CMyLog* pLog = Common::CSingleton<CMyLog>::Instance();
pLog->Log("hello word");
pLog->a = 10;
CMyLog* pLog1 = Common::CSingleton<CMyLog>::Instance();
int b = pLog->a;
system("pause");
return 0;
}
#endif/*SINGLETON_H*/
阅读全文
0 0
- 【Objective-C】单例模式的实现
- Objective-C 单例模式的实现
- c++--Singleton单例模式的实现
- 设计模式--单例模式 C++实现
- 【C++】单例模式的一种简单实现的模板
- Objective —C 的单例模式 Singleton实现
- objective-c 单例模式Singleton的实现
- 单例模式 c语言的实现和使用
- Objective-C 中singleton(单例模式)的实现
- Objective-C 中singleton(单例模式)的实现
- [C#] 静态方式实现的单例是不是懒汉模式?
- [C++] 单件模式的一般实现
- C ++的单例模式
- Objective C 中实现单例模式
- Objective C 实现Singleton(单例)模式.
- Objective-C实现单例模式
- objective-C 实现单例模式
- 单例模式代码实现(C++)
- MySql错误
- 高斯噪声和椒盐噪声
- C++中指针和引用的区别(1)---《More Effective C++》
- 区分大数据、数据分析与数据挖掘
- 详解C#中的反射
- C++单例模式的实现
- 关系型数据库和非关系型数据库
- from data请求ajax
- CentOS 6.5下安装MySQL 5.6
- Git远程操作详解
- 外观模式
- Java中boolean类型占用多少个字节
- Solr集群搭建
- CNN阴影去除--DeshadowNet: A Multi-context Embedding Deep Network for Shadow Removal