ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
我们见到经常有人用 static 局部对象的方式实现了类似单例模式,最近发现一篇文章明确写明 编译器在处理  static局部变量的时候 并不是线程安全的 !!! [http://blogs.msdn.com/b/oldnewthing/archive/2004/03/08/85901.aspx ](http://blogs.msdn.com/b/oldnewthing/archive/2004/03/08/85901.aspx)     于是实现了一个单例工厂  并且是线程安全的 ~~~ #ifndef SINGLETONFACTORY_H #define SINGLETONFACTORY_H #include "windows.h" #include <memory> namespace Tools { template<class T>class SingletonFactory { public: virtual ~SingletonFactory() { ::DeleteCriticalSection(&__criticalSection); } std::auto_ptr<T>& GetInstance(); static SingletonFactory<T>* CreateSingletonFactory(); private: SingletonFactory() { ::InitializeCriticalSection(&__criticalSection); } std::auto_ptr<T> __singletonObj; CRITICAL_SECTION __criticalSection; }; //初始化创建 后续在多线程中使用 //还有另一种写法是单独的函数直接返回内部单例包装静态成员在 多线程情况下不安全 //SingletonFactory::CreateSingletonFactory().GetInstance(); template<class T> SingletonFactory<T>* SingletonFactory<T>::CreateSingletonFactory(){ static SingletonFactory<T> temObj; return &temObj; } //工厂实例 template<class T> std::auto_ptr<T>& SingletonFactory<T>::GetInstance() { if(__singletonObj.get()==0) { ::EnterCriticalSection(&__criticalSection); if(__singletonObj.get()==0) __singletonObj=std::auto_ptr<T>(new T); ::LeaveCriticalSection(&__criticalSection); } return __singletonObj; } } #endif // SINGLETONFACTORY_H ~~~ 测试代码  ~~~ SingletonFactory<Data1>*singleton1=SingletonFactory<Data1>::CreateSingletonFactory(); singleton1->GetInstance()->x=100; cout<<singleton1->GetInstance()->x<<endl; singleton1->GetInstance()->y=200; cout<<singleton1->GetInstance()->x<<endl; cout<<singleton1->GetInstance()->y<<endl; SingletonFactory<Data2>*singleton2=SingletonFactory<Data2>::CreateSingletonFactory(); singleton2->GetInstance()->x=100; cout<<singleton2->GetInstance()->x<<endl; singleton2->GetInstance()->y=200; cout<<singleton2->GetInstance()->x<<endl; cout<<singleton2->GetInstance()->y<<endl; ~~~