Singleton的C++实现 及相关问题

时间:2008-10-24 08:00:03  来源:互联网  作者:  字号:【

编程序的时候很多情况下要求当前的程序中只有一个object。例如一个程序只有一个和数据库的连接,只有一个鼠标的object。

    最简单的方法是用个全局变量或者用个静态变量。但这违反基本的object oriented design 的原则,使程序执行的整体结构,可读性以及可维护大大下降。同时如果所编写的程序不是主程序而是dll的话全局变量的寿命更难控制。


    design pattern 中最简单也是应用最广的就是singleton, 就是用于解决这个问题的。下面是一个简单的singleton的c++的实现,应用这个class之后可以保证当前程序中只有一个copy 。

 

 class singleton

{

public:

static singleton * getinstance()

{
     static singleton instance;

     return &instance;

}

protected:

            singleton();

            singleton();

}


    由于constructor和destructor都是protected,所以无法直接生成这个class。使用时直接用 singleton::getinsgtance()就行了。不必操心singleton的寿命。

另一种实现方法如下:

 

 class singleton

{

public:

static singleton * getinstance()

{
     if(!m_pinstance)
        m_pinstance = new singleton();

     return pinstance;

}

private:

            static singleton *m_pinstance;

protected:

            singleton();

            ~singleton();

}

    这种写法的问题在于你需要在new 之后的适当时候delete 掉这个instance。这个寿命很难控制。但有的人说这个实现是thread_safe的。
而第一个不是thread_safe 。

    我瞧了n天也没有发现这个实现怎么thread safe。经多家讨论后证明这个实现合第一个一样不thread safe。两个进程同时进入getinstance同时m_pinstance还是null,同时constructor花的时间特别长的时候就可能出事。要将其用在多进程的程序中的时候最好在getinstance函数的开始和结束加上“cretical section”。

    当我学完这一段的时候发现他竟然不能用在我的project里,因为我的project里要管理的这个object可能有几个copy(数量确定)。那么就需要把上面的概念稍微扩展一下。把static singleton instance换成数组或者vector。这样能够生成的数量是确定的。使用者不会因为多用几次getinstance而改变了内存的管理。当然用户用 getinstance()的时候应该知道自己要get哪个copy,给getinstance()加个参数。稍微复杂一点。

0

顶一下

0

埋一下

引用地址: