C++单例模式

打印 上一主题 下一主题

主题 542|帖子 542|积分 1626

 C++ 单例模式_c++单例模式-CSDN博客
文章内容来自上面的文章
  1. #include <iostream>
  2. #include "widget.h"
  3. using namespace std;
  4. /*
  5. *        版本1 SingletonPattern_V1 存在以下两个问题
  6. *
  7. *        1. 线程不安全, 非线程安全版本
  8. *        2. 内存泄露
  9. */
  10. class SingletonPattern_V1
  11. {
  12. private:
  13.     SingletonPattern_V1() {
  14.         cout << "constructor called!" << endl;
  15.     }
  16.     SingletonPattern_V1(SingletonPattern_V1&) = delete;
  17.     SingletonPattern_V1& operator=(const SingletonPattern_V1&) = delete;
  18.     static SingletonPattern_V1* m_pInstance;
  19. public:
  20.     ~SingletonPattern_V1() {
  21.         cout << "destructor called!" << endl;
  22.     }
  23.     //在这里实例化
  24.     static SingletonPattern_V1* Instance() {
  25.         if (!m_pInstance) {
  26.             m_pInstance = new SingletonPattern_V1();
  27.         }
  28.         return m_pInstance;
  29.     }
  30.     void use() const { cout << "in use" << endl; }
  31. };
  32. //在类外初始化静态变量
  33. SingletonPattern_V1* SingletonPattern_V1::m_pInstance = nullptr;
  34. //函数入口
  35. int main(int argc, char *argv[])
  36. {
  37.     //测试
  38.     SingletonPattern_V1* p1 = SingletonPattern_V1::Instance();
  39.     SingletonPattern_V1* p2 = SingletonPattern_V1::Instance();
  40.     p1->use();
  41.     p2->use();
  42.     return 0;
  43. }
复制代码
  constructor called!
  in use
  in use
  容易知道此时是内存泄漏的 
线程安全:
多线程中:
  1. static SingletonPattern_V1* Instance() {
  2.                 if (!m_pInstance) {
  3.                         m_pInstance = new SingletonPattern_V1();
  4.                 }
  5.                 return m_pInstance;
  6.         }
复制代码
实行该段代码时,if(!m_pInstance),如果线程A和线程B都判定为空,都开始初始化实例,那么会初始化两次.此时需要加锁.
办理题目后的代码:
  1. #include <iostream>
  2. using namespace std;
  3. #include <memory> // C++11 shared_ptr头文件
  4. #include <mutex>  // C++11 mutex头文件
  5. /*
  6. *        版本2 SingletonPattern_V2 解决了V1中的问题
  7. *
  8. *        1. 通过加锁让线程安全了
  9. *        2. 通过智能指针(shareptr 基于引用计数)内存没有泄露了
  10. */
  11. class SingletonPattern_V2
  12. {
  13. public:
  14.         ~SingletonPattern_V2() {
  15.                 std::cout << "destructor called!" << std::endl;
  16.         }
  17.         SingletonPattern_V2(SingletonPattern_V2&) = delete;
  18.         SingletonPattern_V2& operator=(const SingletonPattern_V2&) = delete;
  19.         //在这里实例化
  20.         static std::shared_ptr<SingletonPattern_V2> Instance()
  21.         {
  22.                 //双重检查锁
  23.                 if (m_pInstance == nullptr) {
  24.                         std::lock_guard<std::mutex> lk(m_mutex);
  25.                         if (m_pInstance == nullptr) {
  26.                                 m_pInstance = std::shared_ptr<SingletonPattern_V2>(new SingletonPattern_V2());
  27.                         }
  28.                 }
  29.                 return m_pInstance;
  30.         }
  31. private:
  32.         SingletonPattern_V2() {
  33.                 std::cout << "constructor called!" << std::endl;
  34.         }
  35.         static std::shared_ptr<SingletonPattern_V2> m_pInstance;
  36.         static std::mutex m_mutex;
  37. };
  38. //在类外初始化静态变量
  39. std::shared_ptr<SingletonPattern_V2> SingletonPattern_V2::m_pInstance = nullptr;
  40. std::mutex SingletonPattern_V2::m_mutex;
  41. int main()
  42. {
  43.         std::shared_ptr<SingletonPattern_V2> p1 = SingletonPattern_V2::Instance();
  44.         std::shared_ptr<SingletonPattern_V2> p2 = SingletonPattern_V2::Instance();
  45.         system("pause");
  46.         return 0;
  47. }
复制代码
用智能共享指针和锁把上面的题目给办理了. 
  1. SingletonPattern_V2(SingletonPattern_V2&) = delete;
复制代码
C++11中=delete的奇妙用法_= delete-CSDN博客


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

曂沅仴駦

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表