Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1774282
  • 博文数量: 198
  • 博客积分: 4088
  • 博客等级: 上校
  • 技术积分: 2391
  • 用 户 组: 普通用户
  • 注册时间: 2011-05-15 16:29
个人简介

游戏开发,系统架构; 博客迁移到:http://www.jianshu.com/u/3ac0504b3b8c

文章分类

全部博文(198)

文章存档

2017年(1)

2016年(12)

2015年(1)

2014年(3)

2013年(13)

2012年(18)

2011年(150)

分类: C/C++

2011-11-23 17:42:07

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <Windows.h>

  4. class TestClass
  5. {
  6. public:
  7.     TestClass()
  8.         :x(0)
  9.     {
  10.         ::Sleep(1000);
  11.         num++;
  12.     }
  13.     ~TestClass(){}
  14.     void Inc() { x++;}
  15.     void Show()
  16.     {
  17.         char result[20] ={0};
  18.         sprintf(result, "x = %d\n", x);
  19.         printf(result);
  20.     }

  21. public:
  22.     static int num;

  23. private:
  24.     int x;
  25.     
  26. };

  27. int TestClass::num = 0;

  28. template <typename T>
  29. class Singleton
  30. {
  31. public:
  32.     static T* getPointer()
  33.     {
  34.         static volatile long beingCreated = false;
  35.         if(m_instance != 0)
  36.         {
  37.             return reinterpret_cast<T*>(m_instance);
  38.         }

  39.         if(!InterlockedExchange(&beingCreated, true))
  40.         {
  41.             T* instance = new T();

  42.             m_instance = reinterpret_cast<void*>(instance);

  43.             InterlockedExchange(&beingCreated, false);

  44.             return instance;
  45.         }

  46.         while(true)
  47.         {
  48.             if(!beingCreated)
  49.             {
  50.                 return reinterpret_cast<T*>(m_instance);
  51.             }

  52.             Sleep(0);
  53.         }
  54.         
  55.     }

  56.     static T & instance()
  57.     {
  58.         return *getPointer();
  59.     }

  60.     static void release()
  61.     {
  62.         if(m_instance)
  63.         {
  64.             delete m_instance;
  65.             m_instance = NULL;
  66.         }
  67.     }

  68. private:
  69.     static void* m_instance;
  70. };

  71. template<typename T>
  72. void* Singleton<T>::m_instance = 0;


  73. HANDLE g_hThreads[3] = {0};


  74. DWORD WINAPI func(LPVOID p)
  75. {
  76.     
  77.     int i = 0;
  78.     while(i < 5)
  79.     {
  80.         Singleton<TestClass>::instance().Inc();
  81.         Singleton<TestClass>::instance().Show();

  82.         Sleep(1);

  83.         ++i;
  84.     }

  85.     return 1;
  86. }

  87. int main()
  88. {
  89.     for(int i = 0; i < 3; ++i)
  90.     {
  91.         g_hThreads[i] = ::CreateThread(NULL, 0, func, NULL, 0, NULL);
  92.     }

  93.     ::WaitForMultipleObjects(3, g_hThreads, TRUE, INFINITE );

  94.     Singleton<TestClass>::release();

  95.     std::cout << "finished" << std::endl;

  96.     std::cout << "has create " << TestClass::num << " TestClass Object!" << std::endl;

  97.     system("pause");

  98.     return 0;
  99. }

程序运行结果:

x = 2
x = 1
x = 3
x = 4
x = 5
x = 6
x = 8
x = 9
x = 7
x = 10
x = 11
x = 12
x = 13
x = 14
x = 15
finished
has create 1 TestClass Object!

最后x = 15, 并且只创建了一个TestClass Object.

倘若将static T* getPointer()改为

  1. static T* getPointer()
  2.     {
  3.         static volatile long beingCreated = false;
  4.         if(m_instance != 0)
  5.         {
  6.             return reinterpret_cast<T*>(m_instance);
  7.         }

  8.         T* instance = new T();

  9.         m_instance = reinterpret_cast<void*>(instance);

  10.         return instance;
  11.     }

则程序运行结果为

x = 1
x = 1
x = 1
x = 3
x = 4
x = 2
x = 5
x = 7
x = 7
x = 8
x = 9
x = 10
x = 11
x = 13
x = 12
finished
has create 3 TestClass Object!

最后的x = 12, 并且创建了3个 TestClass Object,线程不安全,并且违背了单一原则

阅读(2266) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~