设为首页 加入收藏

TOP

C++ Singleton + MultiThread
2015-07-20 17:26:20 来源: 作者: 【 】 浏览:2
Tags:Singleton MultiThread
#include 
  
   
#include 
   
     using namespace std; template 
    
      class Singleton { public: static T *instance() { if (object == NULL) { mtx.lock(); if (object == NULL) object = new T; mtx.unlock(); } return object; } private: Singleton() {} // be here? necessary? static T *object; static mutex mtx; }; template 
     
       T* Singleton
      
       ::object = NULL; template 
       
         mutex Singleton
        
         ::mtx; class Foo { }; int main() { Foo *singletonFoo = Singleton
         
          ::instance(); return 0; }
         
        
       
      
     
    
   
  

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇UVA 10529 Dumb Bones 概率dp 求.. 下一篇HDOJ 4252 A Famous City 单调栈

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·“我用Java 8”已成 (2025-12-26 11:19:54)
·下载 IntelliJ IDEA (2025-12-26 11:19:52)
·Java是什么?(通俗 (2025-12-26 11:19:49)
·雾里看花:真正意义 (2025-12-26 10:54:36)
·C++——模板(超详细 (2025-12-26 10:54:34)