美文网首页
C++单例模式实现(线程安全&支持多参数构造)

C++单例模式实现(线程安全&支持多参数构造)

作者: zagnix | 来源:发表于2020-04-28 20:09 被阅读0次

C++单例模式实现(线程安全&支持多参数构造)

线程安全版本

#include <<mutex>>

template <class T> class ThreadSafeSingleton {
public:
  static T& get() {
    std::call_once(ThreadSafeSingleton<T>::create_once_, &ThreadSafeSingleton<T>::Create);
    return *ThreadSafeSingleton<T>::instance_;
  }
protected:
  static void Create() { instance_ = new T(); }
  static std::once_flag create_once_;
  static T* instance_;
};

template <class T> std::once_flag ThreadSafeSingleton<T>::create_once_;

template <class T> T* ThreadSafeSingleton<T>::instance_ = nullptr;

支持多参数版本的单例类

#include <iostream>
#include <mutex>
#include <cassert>

template<class T>
class ThreadSafeSingleton {

public:
  template<typename...Args>
  static T& Getinstance(Args&&... args) {
    std::call_once(ThreadSafeSingleton<T>::_call_once_flag,
                   std::forward<void(Args&&...)>(&ThreadSafeSingleton<T>::init),
                   std::forward<Args>(args)...);
    return *ThreadSafeSingleton<T>::m_instance;
  }

private:
  ThreadSafeSingleton() = default;
  ~ThreadSafeSingleton() {
    delete ThreadSafeSingleton<T>::m_instance;
    ThreadSafeSingleton<T>::m_instance = nullptr;
  }
  ThreadSafeSingleton(const ThreadSafeSingleton& o) = delete;
  ThreadSafeSingleton& operator=(const ThreadSafeSingleton& o) = delete;
  
  template<typename...Args>
  static void init(Args&&...args) {
    m_instance =  new T(std::forward<Args>(args)...);
  }

private:
  static std::once_flag _call_once_flag;
  static T* m_instance;
};

template<class T> T* ThreadSafeSingleton<T>::m_instance = nullptr;
template<class T> std::once_flag ThreadSafeSingleton<T>::_call_once_flag;

遇到问题点:

  • std::call_once中模板传参时候需要注意点: 第二个参数为函数参数

  • std::forward<函数类型>(具体值)

std::forward<void(Args&&...)>(&ThreadSafeSingleton<T>::init),

最后调用:

  std::call_once(ThreadSafeSingleton<T>::_call_once_flag,

​          std::forward<void(Args&&...)>(&ThreadSafeSingleton<T>::init),

​          std::forward<Args>(args)...);

测试代码:

class TestSingleton1 {
public:
  TestSingleton1(const std::string&){ std::cout << "lvalue" << std::endl;}
  TestSingleton1(std::string&&){ std::cout << "rvalue" << std::endl;}
  ~TestSingleton1() = default;
  void testFunc() {
    std::cout << "test function 1" << "\n";
  }
};

class TestSingleton2 {
public:
  TestSingleton2(const std::string&){ std::cout << "lvalue" << std::endl;}
  TestSingleton2(std::string&&){ std::cout << "rvalue" << std::endl;}
  ~TestSingleton2() = default;
  void testFunc() {
    std::cout << "test function 2" << "\n";
  }
};
class TestSingleton3 {
public:
  TestSingleton3(const std::string&,int i,double k){ std::cout << "lvalue" << std::endl;}
  ~TestSingleton3() = default;
  void testFunc() {
    std::cout << "test function 3" << "\n";
  }
};



int main(int argc, char **argv) {
    std::string str = "bb";
    ThreadSafeSingleton<TestSingleton1>::Getinstance(str).testFunc();
 ThreadSafeSingleton<TestSingleton2>::Getinstance(std::move(std::string("xxxx"))).testFunc();
    ThreadSafeSingleton<TestSingleton3>::Getinstance("yyyy",1,2.0).testFunc();
    return 0
}

本文代码参考

《深入应用C++11》

本文由博客群发一文多发等运营工具平台 OpenWrite 发布

相关文章

  • C++单例模式实现(线程安全&支持多参数构造)

    C++单例模式实现(线程安全&支持多参数构造) 线程安全版本 支持多参数版本的单例类 遇到问题点: std::ca...

  • C++单例模式实现(线程安全&支持多参数构造)

    C++单例模式实现(线程安全&支持多参数构造) 线程安全版本 支持多参数版本的单例类 遇到问题点: std::ca...

  • kotlin实现单例

    java实现单例模式 一直习惯于java的写法,java实现单例主要的思想是构造函数私有,然后考虑线程安全,在实现...

  • 设计模式

    手写单例模式(线程安全) 你知道几种设计模式?单例模式是什么?Spring中怎么实现单例模式?

  • 实现单例模式的方式2

    方式一: 能保证线程安全 定义类的时候实现单例模式 方式二: 能保证线程安全 对已定义好的类实现单例模式

  • 单例模式的几种写法

    一、饿汉式--无法给单例对象的构造方法传递参数,且线程不安全 二、懒汉式--可以给构造函数传递参数,但线程不安全 ...

  • kotlin实现单例模式

    1.懒汉式实现单例模式 2.线程安全懒汉式实现单例模式 3.双重校验懒汉式实现单例模式 4.静态内部类方式实现单例模式

  • 设计模式——单例模式的破坏

    概述: 之前学习了单例模式的几种实现,解决了多线程情况下,单例的线程安全问题,保证了单例的实现。但是单例模式在下面...

  • 单例模式的常用实现方式

    单例模式属于最常用的设计模式,Java中有很多实现单例模式的方式,各有其优缺点 实现方式对比 单例实现方式线程安全...

  • C++设计模式

    单例 单例模式的一种实现(《Effective C++》) 此处是通过C++11新的语义来保证线程的安全性,具体由...

网友评论

      本文标题:C++单例模式实现(线程安全&支持多参数构造)

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