码迷,mamicode.com
首页 > 其他好文 > 详细

Singleton

时间:2014-12-05 10:50:43      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   sp   on   div   log   bs   ad   

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

标签:style   blog   color   sp   on   div   log   bs   ad   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!