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

2020-04-28  本文已影响0人  zagnix

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::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 发布

上一篇下一篇

猜你喜欢

热点阅读