多线程单利模式之双检锁必要性

来源:互联网 发布:evutec手机壳知乎 编辑:程序博客网 时间:2024/06/13 09:05

    static CSingleton* GetInstance()
    {
        if( m_pInstance == NULL )              //优化性能,总比锁快
        {
            CAutoLock  lock( &cs );              //防止多线程引起的同步问题
            if( m_pInstance == NULL )          //确保该段代码进入单线程模式,开始可靠性判断
            {
                m_pInstance  =  new CSingleton;
            }
        }
        return m_pInstance;
    }

 

2014年11月19日15:54:52

0 0