C++实现一个线程安全的单例工厂实现代码
我们见到经常有人用 static 局部对象的方式实现了类似单例模式,最近发现一篇文章明确写明 编译器在处理 static局部变量的时候 并不是线程安全的 !!!
http://blogs.msdn.com/b/oldnewthing/archive/2004/03/08/85901.aspx
于是实现了一个单例工厂 并且是线程安全的
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
#ifndef SINGLETONFACTORY_H
#define SINGLETONFACTORY_H
#include "windows.h"
#include <memory>
namespace Tools
{
template < class T> class SingletonFactory
{
public :
virtual ~SingletonFactory()
{
::DeleteCriticalSection(&__criticalSection);
}
std::auto_ptr<T>& GetInstance();
static SingletonFactory<T>* CreateSingletonFactory();
private :
SingletonFactory()
{
::InitializeCriticalSection(&__criticalSection);
}
std::auto_ptr<T> __singletonObj;
CRITICAL_SECTION __criticalSection;
};
//初始化创建 后续在多线程中使用
//还有另一种写法是单独的函数直接返回内部单例包装静态成员在 多线程情况下不安全
//SingletonFactory::CreateSingletonFactory().GetInstance();
template < class T> SingletonFactory<T>* SingletonFactory<T>::CreateSingletonFactory(){
static SingletonFactory<T> temObj;
return &temObj;
}
//工厂实例
template < class T> std::auto_ptr<T>& SingletonFactory<T>::GetInstance()
{
if (__singletonObj.get()==0)
{
::EnterCriticalSection(&__criticalSection);
if (__singletonObj.get()==0)
__singletonObj=std::auto_ptr<T>( new T);
::LeaveCriticalSection(&__criticalSection);
}
return __singletonObj;
}
}
#endif // SINGLETONFACTORY_H
<br>
|
测试代码
1
2
3
4
5
6
7
8
9
10
11
12
13
|
SingletonFactory<Data1>*singleton1=SingletonFactory<Data1>::CreateSingletonFactory();
singleton1->GetInstance()->x=100;
cout<<singleton1->GetInstance()->x<<endl;
singleton1->GetInstance()->y=200;
cout<<singleton1->GetInstance()->x<<endl;
cout<<singleton1->GetInstance()->y<<endl;
SingletonFactory<Data2>*singleton2=SingletonFactory<Data2>::CreateSingletonFactory();
singleton2->GetInstance()->x=100;
cout<<singleton2->GetInstance()->x<<endl;
singleton2->GetInstance()->y=200;
cout<<singleton2->GetInstance()->x<<endl;
cout<<singleton2->GetInstance()->y<<endl;
|
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
原文链接:http://blog.csdn.net/yue7603835/article/details/49704423