1. 单例模式
线程安全的单例模式, 下面的是抄别人的。
一、懒汉模式:即第一次调用该类实例的时候才产生一个新的该类实例,并在以后仅返回此实例。
需要用锁,来保证其线程安全性:原因:多个线程可能进入判断是否已经存在实例的if语句,从而non thread safety.
使用double-check来保证thread safety.但是如果处理大量数据时,该锁才成为严重的性能瓶颈。
1、静态成员实例的懒汉模式:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
class
Singleton
{
private
:
static
Singleton* m_instance;
Singleton(){}
Singleton&
operator
= (
const
Singleton&);
public
:
static
Singleton* getInstance();
};
Singleton* Singleton::getInstance()
{
if
(NULL == m_instance)
{
Lock();
//借用其它类来实现,如boost
if
(NULL == m_instance)
{
m_instance =
new
Singleton;
}
UnLock();
}
return
m_instance;
}
|
2、内部静态实例的懒汉模式
这里需要注意的是,C++0X以后,要求编译器保证内部静态变量的线程安全性,可以不加锁。但C++ 0X以前,仍需要加锁。
1
2
3
4
5
6
7
8
9
10
11
12
13
|
class
SingletonInside
{
private
:
SingletonInside(){}
public
:
static
SingletonInside* getInstance()
{
Lock();
// not needed after C++0x
static
SingletonInside instance;
UnLock();
// not needed after C++0x
return
instance;
}
};
|
二、饿汉模式:即无论是否调用该类的实例,在程序开始时就会产生一个该类的实例,并在以后仅返回此实例。
由静态初始化实例保证其线程安全性,WHY?因为静态实例初始化在程序开始时进入主函数之前就由主线程以单线程方式完成了初始化,不必担心多线程问题。
故在性能需求较高时,应使用这种模式,避免频繁的锁争夺。
|
class CInstance
{
private:
static CInstance* m_spInstance;
CInstance(){}
public:
static CInstance* getInstance()
{
return m_spInstance;
}
};
CInstance* CInstance::m_spInstance = new CInstance();
总结:
1. 构造函数要私有化。在类外不能创建对象,但是在静态成员方法getInstance中是可以的。
2.注意线程安全
3.要有Destory释放接口