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

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

线程安全版本

#include <>

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

template  std::once_flag ThreadSafeSingleton::create_once_;

template  T* ThreadSafeSingleton::instance_ = nullptr;

支持多参数版本的单例类

#include 
#include 
#include 

template
class ThreadSafeSingleton {

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

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

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

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

遇到问题点:

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

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

std::forward(&ThreadSafeSingleton::init),

最后调用:

  std::call_once(ThreadSafeSingleton::_call_once_flag,

​          std::forward(&ThreadSafeSingleton::init),

​          std::forward(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::Getinstance(str).testFunc();
 ThreadSafeSingleton::Getinstance(std::move(std::string("xxxx"))).testFunc();
    ThreadSafeSingleton::Getinstance("yyyy",1,2.0).testFunc();
    return 0
}

本文代码参考

《深入应用C++11》

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

你可能感兴趣的:(C++单例模式实现(线程安全&支持多参数构造))