posts - 45,  comments - 232,  trackbacks - 0

Singleton模式是常用的设计模式之一,但是要实现一个真正实用的设计模式却也不是件容易的事情。

1.         标准的实现

class Singleton

{

public:

       static Singleton * Instance()

       {

              if( 0== _instance)

              {

                     _instance = new Singleton;

              }

              return _instance;

       }

protected:

       Singleton(void)

       {

       }

       virtual ~Singleton(void)

       {

       }

       static Singleton* _instance;

};

       这是教科书上使用的方法。看起来没有什么问题,其实包含很多的问题。下面我们一个一个的解决。

2.         自动垃圾回收

上面的程序必须记住在程序结束的时候,释放内存。为了让它自动的释放内存,我们引入auto_ptr改变它。

#include <memory>

#include <iostream>

using namespace std;

class Singleton

{

public:

       static Singleton * Instance()

       {

              if( 0== _instance.get())

              {

                     _instance.reset( new Singleton);

              }

              return _instance.get();

       }

protected:

       Singleton(void)

       {

              cout <<"Create Singleton"<<endl;

       }

       virtual ~Singleton(void)

       {

              cout << "Destroy Singleton"<<endl;

       }

       friend class auto_ptr<Singleton>;

       static auto_ptr<Singleton> _instance;

};

//Singleton.cpp

auto_ptr<Singleton> Singleton::_instance;

3.         增加模板

在我的一个工程中,有多个的Singleton类,对Singleton类,我都要实现上面这一切,这让我觉得烦死了。于是我想到了模板来完成这些重复的工作。

现在我们要添加本文中最吸引人单件实现:

/********************************************************************

    (c) 2003-2005 C2217 Studio

    Module:    Singleton.h

    Author:     Yangjun D.

    Created:    9/3/2005   23:17

    Purpose:    Implement singleton pattern

    History:

*********************************************************************/

#pragma once

 

#include <memory>

using namespace std;

using namespace C2217::Win32;

 

namespace C2217

{

namespace Pattern

{

template <class T>

class Singleton

{

public:

       static inline T* instance();

      

private:

       Singleton(void){}

       ~Singleton(void){}

       Singleton(const Singleton&){}

       Singleton & operator= (const Singleton &){}

 

       static auto_ptr<T> _instance;

};

 

template <class T>

auto_ptr<T> Singleton<T>::_instance;

 

template <class T>

 inline T* Singleton<T>::instance()

{

       if( 0== _instance.get())

       {

              _instance.reset ( new T);

       }

      

       return _instance.get();

}

 

//Class that will implement the singleton mode,

//must use the macro in it's delare file

#define DECLARE_SINGLETON_CLASS( type ) \

       friend class auto_ptr< type >;\

       friend class Singleton< type >;

}

}

 

4.         线程安全

上面的程序可以适应单线程的程序。但是如果把它用到多线程的程序就会发生问题。主要的问题在于同时执行_instance.reset ( new T); 就会同时产生两个新的对象,然后马上释放一个,这跟Singleton模式的本意不符。所以,你需要更加安全的版本:

/********************************************************************

    (c) 2003-2005 C2217 Studio

    Module:    Singleton.h

    Author:     Yangjun D.

    Created:    9/3/2005   23:17

    Purpose:    Implement singleton pattern

    History:

*********************************************************************/

#pragma once

 

#include <memory>

using namespace std;

#include "Interlocked.h"

using namespace C2217::Win32;

 

namespace C2217

{

namespace Pattern

{

template <class T>

class Singleton

{

public:

       static inline T* instance();

      

private:

       Singleton(void){}

       ~Singleton(void){}

       Singleton(const Singleton&){}

       Singleton & operator= (const Singleton &){}

 

       static auto_ptr<T> _instance;

       static CResGuard _rs;

};

 

template <class T>

auto_ptr<T> Singleton<T>::_instance;

 

template <class T>

CResGuard Singleton<T>::_rs;

 

template <class T>

 inline T* Singleton<T>::instance()

{

       if( 0 == _instance.get() )

       {

              CResGuard::CGuard gd(_rs);

              if( 0== _instance.get())

              {

                     _instance.reset ( new T);

              }

       }

       return _instance.get();

}

 

//Class that will implement the singleton mode,

//must use the macro in it's delare file

#define DECLARE_SINGLETON_CLASS( type ) \

       friend class auto_ptr< type >;\

       friend class Singleton< type >;

}

}

       CresGuard 类主要的功能是线程访问同步,代码如下:

/******************************************************************************

Module:  Interlocked.h

Notices: Copyright (c) 2000 Jeffrey Richter

******************************************************************************/

 

 

#pragma once

///////////////////////////////////////////////////////////////////////////////

 

// Instances of this class will be accessed by multiple threads. So,

// all members of this class (except the constructor and destructor)

// must be thread-safe.

class CResGuard {

public:

   CResGuard()  { m_lGrdCnt = 0; InitializeCriticalSection(&m_cs); }

   ~CResGuard() { DeleteCriticalSection(&m_cs); }

 

   // IsGuarded is used for debugging

   BOOL IsGuarded() const { return(m_lGrdCnt > 0); }

 

public:

   class CGuard {

   public:

      CGuard(CResGuard& rg) : m_rg(rg) { m_rg.Guard(); };

      ~CGuard() { m_rg.Unguard(); }

 

   private:

      CResGuard& m_rg;

   };

 

private:

   void Guard()   { EnterCriticalSection(&m_cs); m_lGrdCnt++; }

   void Unguard() { m_lGrdCnt--; LeaveCriticalSection(&m_cs); }

 

   // Guard/Unguard can only be accessed by the nested CGuard class.

   friend class CResGuard::CGuard;

 

private:

   CRITICAL_SECTION m_cs;

   long m_lGrdCnt;   // # of EnterCriticalSection calls

};

 

 

///////////////////////////////////////////////////////////////////////////////

5.         实用方法

比如你有一个需要实现单件模式的类,就应该这样实现:

#pragma once

#include "singleton.h"

using namespace C2217::Pattern;

 

class ServiceManger

{

public:

       void Run()

       {

       }

private:

       ServiceManger(void)

       {

       }

       virtual ~ServiceManger(void)

       {

       }

       DECLARE_SINGLETON_CLASS(ServiceManger);

};

 

typedef Singleton<ServiceManger> SSManger;

 

在使用的时候很简单,跟一般的Singleton实现的方法没有什么不同。

int _tmain(int argc, _TCHAR* argv[])

{

        SSManger::instance()->Run();

}

 

一个简单的Singleton模式的实现,可以看到C++语言背后隐藏的丰富的语意,我希望有人能实现一个更好的Singleton让大家学习。我从一开始实现Singleton类的过程,其实就是我学习C++的过程,越是深入越觉得C++了不起。

 

 

posted on 2005-09-20 22:14 天下无双 阅读(55305) 评论(36)  编辑 收藏 引用 所属分类: C/C++

FeedBack:
# re: C++完美实现Singleton模式
2005-09-21 00:31 | mexo
似乎可以这样:
template <class T>
class Singleton
{
public:
static inline T& instance()
{
static T _instance;
return _instance;
}

private:
Singleton(void);
~Singleton(void);
Singleton(const Singleton<T>&);
Singleton<T>& operator= (const Singleton<T> &);
};
  回复  更多评论
  
# re: C++完美实现Singleton模式
2005-09-21 08:52 | 天下无双
Cool!
比我的实现简单很多,并且也达到了同样的效果,不佩服都不行.  回复  更多评论
  
# re: C++完美实现Singleton模式
2005-12-22 18:08 | 小明
mexo 说的实现方式Meyer(Effective c++)提出来的

这种方式并不是没有多线程问题的  回复  更多评论
  
# 还不够完美
2006-01-09 21:51 | NoSound
mexo提出的方案的确不错,但是好象也并不完美(我不是指多线程解决方案),因为他把模板类的构造函数放在私有段里了,如果放在protected段里就好得多,因为你的类可以从模板类继承,这样就不再需要你的那个 typedef Singleton<ServiceManger> SSManger;定义了。示例如下:
template <class T>
class Singleton {
public:
static T& instance() {
static T _instance;
return _instance;
}
protected:
Singleton(void) {}
virtual ~Singleton(void) {}
Singleton(const Singleton<T>&); //不实现
Singleton<T>& operator= (const Singleton<T> &); //不实现
};
--------------------------------
下面是一个需要做为单例的类,只需从Singleton继承即可
class Test : public Singleton<Test> {
public:
void foo();
private:
Test();
~Test();
friend class Singleton<Test>;
};
----------------------
这样,别人在使用的时候,只需要写
Test::instance().foo();
而再也不需要写:
Singleton<Test>::instance().foo();
或者
typedef Singleton<Test> STest;
STest::instance().foo();
-----------------------------
  回复  更多评论
  
# re: C++完美实现Singleton模式
2006-01-17 16:22 | 3×7=51
我猜楼主一定没看过Andrei Alexandrescu写的Modern C++ Design  回复  更多评论
  
# re: C++完美实现Singleton模式
2006-01-18 09:02 | 天下无双
我是写些Singleton的实现, 后来有机会看来你说的那本书, 感觉思想差不多,不谋而和。  回复  更多评论
  
# re: C++完美实现Singleton模式
2006-01-27 11:07 | cyt
Singleton在多线程方面,还漏了一篇文章没有看:
《Double-Checked Locking,Threads,Compiler Optimizations,and More》(Scott Meyers)

主要意思就是在:_instance.reset ( new T);
在编译器优化情况下,_instance先设了指针内容,然后再进行构造函数。如果有第二个线程这时候进行访问,_instance内容为非空,于是跳过了第一个if( 0 == _instance.get() )。但实际上对象还是没有构造完整。

实际上我们多是先用
std::auto_ptr<T> _au(new T);
_instance = _au;
  回复  更多评论
  
# re: C++完美实现Singleton模式
2006-03-23 17:12 | cat5
如果已经作了多线程保护,_instance.reset ( new T); 和std::auto_ptr<T> _au(new T);
_instance = _au;
不一样吗?  回复  更多评论
  
# re: C++完美实现Singleton模式
2006-03-23 18:33 | cat5
Sorry,
if( 0 == _instance.get() )
{
CResGuard::CGuard gd(_rs);
if( 0== _instance.get())
{
_instance.reset ( new T);
}
如果把CResGuard::CGuard gd(_rs);放到第一句即可解决,但却带来了效率问题。这就是DCL要解决的内容。  回复  更多评论
  
# re: C++完美实现Singleton模式
2006-12-18 16:45 | DENGYangjun
继承的方法还是比较好用的,现在又体会了。但有时候继承显得比较乱,好像那个情形都有自己适用的情形。  回复  更多评论
  
# re: C++完美实现Singleton模式
2007-01-29 14:32 | 李锦俊
用智能指针,意味着不能自己控制何时释放,如果几个不同的Singleton需要按顺序创建并且释放,这种实现方式就不太好了  回复  更多评论
  
# re: C++完美实现Singleton模式
2007-01-29 18:36 | DENGYangjun
如果你把指针置空,就会释放对象,这个应该不是问题。可以加一个这样的接口。  回复  更多评论
  
# re: C++完美实现Singleton模式
2007-02-06 15:19 | sz
本着简单高效的原则, boost::singleton是singleton模式的又一种实现, 它基于以下假设, 一个良好的设计, 在进入main函数前应该是单线程的,此时,我们可以采用和全局变量相似的办法来使用singleton,因为所有的全局变量在进入main以前已经全部初始化。这样我们就避开了多线程的竞争条件. 但直接使用全局变量有一个严重的缺陷,就是当你使用全局变量时,你并不能保证它已经得到初始化,这种情况发生在 static code中。(static code是boost文档中的用语, 我想它是指在进入main函数以前要执行的代码)。

boost::singlton实现的关键有两点
(1) sington 在进入main函数前初始化.
(2)第一次使用时, singlton已得到正确的初始化(包括在static code中情况).

 boost中的实现代码如下所示:
 
template <typename T>
struct singleton
{
private:
struct object_creator
{
object_creator() { singleton<T>::instance(); }
inline void do_nothing() const { }
};
static object_creator create_object;
singleton();
public:
typedef T object_type;
static object_type & instance()
{
static object_type obj;
create_object.do_nothing();
return obj;
}
};

template <typename T> typename singleton<T>::object_creator singleton<T>::create_object;

我没看明白,谁能给分析分析呀!  回复  更多评论
  
# re: C++完美实现Singleton模式
2007-02-08 11:25 | 天下无双
我想他的意思就是在主函数运行以前,初始化单件类,确实能够克服多线程的问题。不过呢,这就失去了,惰性初始化的优点-即需要的用的时候才初始化。  回复  更多评论
  
# re: C++完美实现Singleton模式
2007-05-23 00:31 | ??
@_@程序用了大半天才开始用singleton的情况多么?  回复  更多评论
  
# re: C++完美实现Singleton模式
2007-05-23 01:03 | eXile
这种方式远谈不上完美,一般C++的singleton实现的最大缺陷就是释放的顺序依赖问题,而象ModerC++Design那样,又太复杂了,不实用。
所以,我现在都是用main()中的局部变量来模拟全局变量,能很好用于单件模式 。  回复  更多评论
  
# re: C++完美实现Singleton模式
2007-05-23 01:07 | eXile
而且前一向老外有一篇文章,说了一下什么情况下,双重锁会失败,我没有细看,总之也是不安全的  回复  更多评论
  
# re: C++完美实现Singleton模式
2007-06-05 15:23 | 天下无双
请eXile讲讲具体怎么用局部变量模拟全局变量。谢谢  回复  更多评论
  
# re: C++完美实现Singleton模式
2007-08-25 18:09 | 蚂蚁终结者
大概看了一下,除了cyt说的Double-Checked Locking,编译器可能会混乱代码的执行次序,即先设置_instance指针的内容再执行构造函数。
还至少有两个问题:
1.
auto_ptr在某些情况下会出问题,假设有某个单例类A在析构时调用另外一个单例类Log来记录一些日志信息,因为在程序结束时静态成员的析构可能会是任意次序,单例类Log很有可能在A调用析构函数之前就析构了,后果就不用说吧。

当然解决方法很简单,用C标准库的atexit就行了,atexit函数原型如下:
int atexit(void (*func )());
用atexit可以注册任意多个函数,当程序结束时会按LIFO的次序调用注册的函数。这样就能保证多个有依赖关系的单例类的析构顺序。
我们修改Singleton的实现,加上:
static void Destroy() {
if ( _instance != 0 ) {
delete _instance;
_instance = 0;
}
}
将Instance实现修改为类似代码:
static T& Instance() {
if (0 == _instance) {
Lock lock(_cs);
if (0 == _instance) {
_instance = new T();
atexit(Destroy);
}
}
return *_instance;
}

2.

_instance.reset ( new T);
或者
_instance = new T();
这里其实还会有问题,在C++中对指针赋值操作并不能保证是原子操作,如果有某个线程1执行到这里,赋值到一半,线程1挂起,线程2开始执行,这时候
_instance可能处于任何状态,0 == _instance 也许为true,线程2于是return *_instance,这时候就会有问题了...

设计一个完美的Singleton也许比想象的要难的多  回复  更多评论
  
# re: C++完美实现Singleton模式
2007-08-25 18:22 | 蚂蚁终结者
貌似还有一点忘了,就是防治编译器多线程环境下的优化,
这正是volatile关键词的用处

static auto_ptr<T> _instance;
或者用atexit后改成
static T * _instance;
都可能会有问题,因为多线程环境下的变量容易被缓存
所以最好加上volatile

static volatile auto_ptr<T> _instance;
或者用atexit后改成
static T * volatile _instance;  回复  更多评论
  
# re: C++完美实现Singleton模式
2007-10-24 09:14 | 老五
# re: C++完美实现Singleton模式
2007-10-30 16:28 | chjw_ch
这种方法是不行的在某些情况下是不能够使用的,例如
class SingletonClass : public Singleton <SingletonClass>
{
....
};

@mexo
  回复  更多评论
  
# re: C++完美实现Singleton模式
2007-10-30 16:30 | chjw_ch
这种方法是不行的在某些情况下是不能够使用的,例如
class SingletonClass : public Singleton <SingletonClass>
{
....
};
针对的是
# re: C++完美实现Singleton模式 2005-09-21 00:31 | mexo
提出的方案  回复  更多评论
  
# re: C++完美实现Singleton模式
2008-03-25 20:33 | kendan
学习了。。。。。  回复  更多评论
  
# re: C++完美实现Singleton模式
2009-03-09 09:43 | madmanahong
别老研究Singleton了,没啥意义,你的代码中越多运用了Singleton,越表示你的设计有问题!

只要为什么?去看《重构与模式》这本书!  回复  更多评论
  
# re: C++完美实现Singleton模式
2009-04-17 10:45 | 创意产品网
不错。  回复  更多评论
  
# re: C++完美实现Singleton模式
2010-01-18 16:19 | lxchust
明显的错误:析构函数被限定为protected怎么调用?如何析构  回复  更多评论
  
# re: C++完美实现Singleton模式
2010-05-18 09:14 | QQ:124294272
@NoSound
程序调试通不啊,小兄弟  回复  更多评论
  
# re: C++完美实现Singleton模式
2010-07-27 17:00 | foratiw
@NoSound
恩,这个方法很简洁  回复  更多评论
  
# re: C++完美实现Singleton模式
2010-12-10 12:03 | 艾克斯の編碼者
我是这样的
#pragma once
#ifndef SINGLETON_H
#define SINGLETON_H

template<class T>
class Singleton
{
public:
static T& Instance();

protected:
Singleton(){}
virtual ~Singleton(){}

/**
* 防止拷贝复制
*/
private:
Singleton(const Singleton &);
Singleton & operator = (const Singleton &);
};

template
T& Singleton::Instance()
{
/** 建立一个静态对象 */
static T instance;
return instance;
}

#endif  回复  更多评论
  
# re: C++完美实现Singleton模式
2011-08-08 18:43 | 爱因斯坦
@蚂蚁终结者
指针赋值不会赋值到一半问题。
只是构造可能晚于指针赋值  回复  更多评论
  
# re: C++完美实现Singleton模式
2011-09-26 23:06 | 过客
貌似离完美还差很远,double-check本身就不能解决多线程问题,有关析构或构造失败的问题都没有考虑进去,而且auto_ptr本身就不完美  回复  更多评论
  
# re: C++完美实现Singleton模式
2012-01-21 15:14 | Setekhid
你这第一个范例对么?怎么我的codeblock不通过??  回复  更多评论
  
# re: C++完美实现Singleton模式
2012-02-18 23:16 | seckcoder
文章很精彩. 评论更精彩  回复  更多评论
  
# re: C++完美实现Singleton模式
2012-09-08 09:06 | Wilson
@mexo
如果Singleton仅仅如此,那么很多的先锋们真的早就该死了。可悲的不是如此。
请问此方法如何实现延后实例化?
  回复  更多评论
  
# re: C++完美实现Singleton模式
2014-11-18 16:45 | 过客
isGuarded似乎忘记调用了。在new一个新的instance前应该注意该线程是否可用。  回复  更多评论
  

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理



常用链接

留言簿(15)

随笔分类

随笔档案

相册

我的其它领域Blog

搜索

  •  

积分与排名

  • 积分 - 202567
  • 排名 - 128

最新评论

阅读排行榜

评论排行榜