我们来看看利用Singlton模式来编程的几个细节.
首先是线程安全问题, 我们的Instance方法只在第一次创建类的实例, 但是如果有两个线程同时访问Instance. 则存在潜在风险.
来分析一下, 假设有两个线程A, B. 当A进入Instance(), 发现该对象还没有创建, 进入if()分支. 此时由于时间片到或者被抢占等原因, 线程A被挂起, 开始运行线程B. 此时B也调用Instance()同时也发现对象没有创建, 进入if()分支创建了一个类对象. 二当A线程再次运行回来的时候它会再创建一个类对象. 最终B线程创建的类对象, 我们已经没有办法再找到他了, 造成了内存的泄露.
解决这个问题的办法很显然就是加入线程资源同步的手段. 为了跟开发平台无关, 我们 假设有一个Mutex互斥信号量的类. 看下面的改进代码.
static T *m_pInstance;
static Mutex m_Mutex;
};
template<typename T>
T *Singleton<T>::m_pInstance = NULL;
template<typename T>
Mutex Singleton<T>::m_Mutex;
由于增加了互斥信号量的保护, 同一时间只有一个线程可以进入到临界代码段, 确保了安全性.
我们再来看效率问题, 绝大部分时候调用Instance方法去获得类实例时, 都会执行Lock, Unlock. 而我们设计的本意是保证这个对象还没创建的时候才需要保护. 因此我们可以在Lock机制外面再增加一次对资源的判断, 完美的解决了这个问题.
static T *m_pInstance;
static Mutex m_Mutex;
};
template<typename T>
T *Singleton<T>::m_pInstance = NULL;
template<typename T>
Mutex Singleton<T>::m_Mutex;
接下来让我们关注一下内存的问题, 首先理解二点:
第一, 这里声明Singleton<T>模板类的构造,析构为private的, 因为我们不需要创建任何一个Singleton<T>的对象. 我们只是通过它的接口去创建具体的某种功能类实例.
第二, 对于Singleton<T>类中的静态成员我们不必担心他们本身的内存问题, 因为静态成员与全局变量是一样的, 构造和析构是在main()之前和之后自动调用.
因此我们要关注的就是被创建的功能类如何释放的问题. 在上一篇中我将所有功能类的析构声明为public. 让在main的最后面使用delete来删除所创建的对象. 这样看似解决了内存泄露的问题. 但是在实际应用中我们会很多地方要使用到功能类. 如何去确定删除的时机? 这是很头疼的问题. 能想到的最好的方法就是让这个对象在main之后自动析构. 为了达到这个目的, 我们使用auto_ptr<T>这个智能指针模板类来管理功能类对象的内存. 看下面代码块
using namespace std;
template<typename T>
class Singleton
{
public:
static T *Instance (void)
{
if (m_pInstance.get() == NULL)
{
m_Mutex.Lock();
if(m_pInstance.get() == NULL)
{
m_pInstance.reset(new T);
}
m_Mutex.Unlock();
}
return m_pInstance.get();
}
private:
Singleton (void) {}
~Singleton (void) {}
static auto_ptr<T> m_pInstance;
static Mutex m_Mutex;
};
template<typename T>
auto_ptr<T> Singleton<T>::m_pInstance;
template<typename T>
Mutex Singleton<T>::m_Mutex;
class Mouse
{
public:
void Move (void) { cout << "Move Mouse" << endl; }
private:
Mouse (void) { cout << "Create Mouse" << endl; }
~Mouse (void) { cout << "Destory Mouse" << endl; }
friend class auto_ptr<Mouse>;
friend class Singleton<Mouse>;
};
class Printer
{
public:
void Print (void) { cout << "Print pape" << endl; }
private:
Printer (void) { cout << "Create Printer" << endl; }
~Printer (void) { cout << "Destory Printer" << endl; }
friend class auto_ptr<Printer>;
friend class Singleton<Printer>;
};
int main (void)
{
Mouse *p1 = Singleton<Mouse>::Instance(); // 控制台 : Create Mouse
p1->Move(); // 控制台 : Move Mouse
Printer *p2 = Singleton<Printer>::Instance(); // 控制台 : Create Printer
p2->Print(); // 控制台 : Move Printer
return 0;
// auto_ptr<T>自动析构p1, p2指向的对象
}
上面将所有功能类的析构声明private, 这里外部不能任意去delete. 同时将auto_ptr<T>声明为功能类的友元, 使得auto_ptr的析构中能够去调用功能类的private析构函数. 从而实现内存释放.
好了, 说了这么多, 最终给出一个Ready to Run的Singleton.h并用一个main.cpp例子演示.
Singleton.h
#include <iostream>
#include <memory>
#include "Mutex.h"
using namespace std;
template<typename T>
class Singleton
{
public:
static T *Instance (void)
{
if (m_pInstance.get() == NULL)
{
m_Mutex.Lock();
if(m_pInstance.get() == NULL)
{
m_pInstance.reset(new T);
}
m_Mutex.Unlock();
}
return m_pInstance.get();
}
private:
Singleton (void) {}
~Singleton (void) {}
static auto_ptr<T> m_pInstance;
static Mutex m_Mutex;
};
template<typename T>
auto_ptr<T> Singleton<T>::m_pInstance;
template<typename T>
Mutex Singleton<T>::m_Mutex;
#define DECLARE_SINGLETON_CLASS(type) /
friend class auto_ptr<type>;/
friend class Singleton<type>
#endif;
main.cpp
class Mouse
{
public:
void Move (void) { cout << "Move Mouse" << endl; }
private:
Mouse (void) { cout << "Create Mouse" << endl; }
~Mouse (void) { cout << "Destory Mouse" << endl; }
DECLARE_SINGLETON_CLASS(Mouse);
};
typedef Singleton<Mouse> MouseSingleton;
class Printer
{
public:
void Print (void) { cout << "Print pape" << endl; }
private:
Printer (void) { cout << "Create Printer" << endl; }
~Printer (void) { cout << "Destory Printer" << endl; }
DECLARE_SINGLETON_CLASS(Printer);
};
typedef Singleton<Printer> PrinterSingleton;
int main (void)
{
Mouse *p1 = MouseSingleton::Instance(); // 控制台 : Create Mouse
p1->Move(); // 控制台 : Move Mouse
Printer *p2 = PrinterSingleton::Instance(); // 控制台 : Create Printer
p2->Print(); // 控制台 : Move Printer
return 0;
// 自动析构p1, p2指向的对象
}