您的位置:首页 > 编程语言 > C语言/C++

C++之单例模式代码实现

2017-08-24 10:54 134 查看
template <typename Type>

class Singleton

{

public:

 static Type* getInstance()

 {

  if (!_instance)

  {

   unique_lock<mutex> lock(_mutex);

   if (!_instance)

    _instance = make_unique<Type>();

  }

  return _instance.get();

 }
private:

 static unique_ptr<Type> _instance;

 static mutex _mutex;

};

#define IMPLEMENT_SINGLETON(Type) \

unique_ptr<Type> Singleton<Type>::_instance; \

mutex Singleton<Type>::_mutex;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: