[关闭]
@lupnfer 2017-03-06T12:37:51.000000Z 字数 2336 阅读 805

singleton

Code


singleton

Q(TODO): Args&& ... args 跟 Args& ... args的区别

c++11

  1. /************************************************************************
  2. $Id: singleton.h 1963 2016-03-01 12:12:37Z $
  3. Product:
  4. Project:
  5. Created:
  6. Authors:
  7. Description: common singleton
  8. -------------------------------------------------------------------------
  9. ChangeLog
  10. DATE NAME DESCRIPTION
  11. -------------------------------------------------------------------------
  12. ************************************************************************/
  13. #ifndef WISEOS_COMMON_SINGLETON_H_
  14. #define WISEOS_COMMON_SINGLETON_H_
  15. #include <string>
  16. #include <iostream>
  17. #include <memory>
  18. #include <mutex>
  19. namespace wiseos {
  20. namespace common {
  21. template <typename T>
  22. class Singleton {
  23. public:
  24. template<typename... Args>
  25. //static T &GetInstance(Args& ... args) diff c++ primer
  26. static inline std::shared_ptr<T> instance(Args&& ... args) {
  27. // using double-check-lock.
  28. if (!instance_.get()) {
  29. std::unique_lock<std::mutex> lock(instanceMutex_);
  30. if (!instance_.get()) {
  31. instance_.reset(new T(std::forward<Args>(args)...));
  32. }
  33. }
  34. return instance_;
  35. }
  36. private:
  37. Singleton() = default;
  38. virtual ~Singleton() = default;
  39. Singleton(const Singleton&) = default;
  40. Singleton& operator = (const Singleton &) = delete;
  41. // instance
  42. static std::shared_ptr<T> instance_;
  43. static std::mutex instanceMutex_;
  44. };
  45. } // namespace common
  46. } // namespace wiseos
  47. template <typename T>
  48. std::shared_ptr<T> wiseos::common::Singleton<T>::instance_;
  49. template <typename T>
  50. std::mutex wiseos::common::Singleton<T>::instanceMutex_;
  51. #define SINGLETON_DECL(type) \
  52. friend class std::shared_ptr< type >; \
  53. friend class wiseos::common::Singleton< type >;
  54. #endif // WISEOS_COMMON_SINGLETON_H_

-lboost_thread -boost_system
C++03

  1. #ifndef _SINGLETON_H_
  2. #define _SINGLETON_H_
  3. #include <string>
  4. #include <boost/thread.hpp>
  5. using namespace std;
  6. template < typename T>
  7. class Singleton
  8. {
  9. public :
  10. template<typename... Args>
  11. static T &GetInstance(Args& ... args) {
  12. // using double-check-lock.
  13. if(instance_ == NULL) {
  14. boost::unique_lock<boost::mutex> lock(instanceMutex_);
  15. if(instance_ == NULL) {
  16. instance_ = new T(args ...);
  17. atexit(Destroy); //register fun when process exit
  18. }
  19. }
  20. return *instance_;
  21. }
  22. private :
  23. static void Destroy()
  24. {
  25. delete instance_;
  26. instance_ = NULL;
  27. }
  28. Singleton(const Singleton &other);
  29. Singleton & operator = (const Singleton &other);
  30. Singleton();
  31. ~Singleton();
  32. static T *instance_;
  33. static boost::mutex instanceMutex_;
  34. };
  35. //static member init
  36. template < typename T>
  37. T *Singleton<T>::instance_ = NULL;
  38. template <typename T>
  39. boost::mutex Singleton<T>::instanceMutex_;
  40. #endif // _SINGLETON_H_
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注