标签:
2015-09-19
1 #include <stdio.h> 2 //单例模式 3 class Singleton 4 { 5 private: 6 Singleton(){} 7 ~Singleton(){delete singleton;} 8 static Singleton *singleton; 9 public: 10 static Singleton *GetInstance() 11 { 12 return singleton; 13 } 14 }; 15 Singleton *Singleton::singleton=new Singleton;//静态变量初始化 16 void main() 17 { 18 Singleton *singleton1=Singleton::GetInstance(); 19 Singleton*singleton2=Singleton::GetInstance(); 20 printf("%s\n",singleton1==singleton2?"true":"false"); 21 }
这样即是在多线程中,也不会造成同步问题。
标签:
原文地址:http://www.cnblogs.com/dalgleish/p/4821546.html