首页 > 其他 > 详细

Singleton

时间:2014-12-05 10:50:23      阅读:221      评论:0      收藏:0      [点我收藏+]
class Singleton
{
public:
    ~Singleton();
    static Singleton* GetInstance();

private:
    Singleton();
    static Singleton* m_pInstance;
    static pthread_mutex_t s_initLock;

    class Deletor
    {
    public:
        ~Deletor()
        {
            if (Singleton::m_pInstance)
            {
                delete Singleton::m_pInstance;
                Singleton::m_pInstance = NULL;
            }
        }
    };
    static Deletor s_deletor;
};



Singleton* Singleton::m_pInstance;
pthread_mutex_t Singleton::s_initLock = PTHREAD_MUTEX_INITIALIZER;
Singleton::Deletor Singleton::s_deletor;


Singleton::Singleton()
{
}

Singleton::~Singleton()
{
}


Singleton* Singleton::GetInstance()
{
    if (!m_pInstance)
    {
        pthread_mutex_lock(&s_initLock);
        if (!m_pInstance)
        {
            m_pInstance = new Singleton;
        }
        pthread_mutex_unlock(&s_initLock);
    }

    return m_pInstance;
}

 

Singleton

原文:http://www.cnblogs.com/stanley198610281217/p/4146044.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!