美文网首页
C++设计模式

C++设计模式

作者: TocomPass | 来源:发表于2018-06-04 10:54 被阅读0次

    单例

    单例模式的一种实现(《Effective C++》)

    class S
    {
        public:
            static S& getInstance()
            {
                static S instance; 
                return instance;
            }
        private:
            S() {};             
    
            S(S const&)               = delete;
            void operator=(S const&)  = delete;
    };
    

    此处是通过C++11新的语义来保证线程的安全性,具体由编译器去处理,编译器可能使用DCLP的方式或者其他的方式完成。

    The C++11 standard §6.7.4:
    If control enters the declaration concurrently while the variable is being initialized, the concurrent execution shall wait for completion of the initialization.

    C++内存模型和单例以及双重检查的问题

    相关文章

      网友评论

          本文标题:C++设计模式

          本文链接:https://www.haomeiwen.com/subject/mnyesftx.html