Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1778935
  • 博文数量: 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 18:38:53

  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. class CLock
  29. {
  30. public:
  31.     CLock()
  32.     {
  33.         ::InitializeCriticalSection(&m_Section);
  34.     }
  35.     ~CLock()
  36.     {
  37.         ::DeleteCriticalSection(&m_Section);
  38.     }

  39.     void Lock()
  40.     {
  41.         ::EnterCriticalSection(&m_Section);
  42.     }

  43.     void UnLock()
  44.     {
  45.         ::LeaveCriticalSection(&m_Section);
  46.     }

  47. private:
  48.     CRITICAL_SECTION m_Section;
  49. };

  50. template <typename T>
  51. class Singleton
  52. {
  53. public:
  54.     static T* getPointer()
  55.     {
  56.         if(m_instance == NULL)
  57.         {
  58.             m_Lock.Lock();

  59.             if(m_instance == NULL)
  60.             {

  61.                 T* instance = new T();

  62.                 m_instance = reinterpret_cast<void*>(instance);
  63.             }

  64.             m_Lock.UnLock();
  65.         }

  66.         return reinterpret_cast<T*>(m_instance);
  67.         
  68.     }

  69.     static T & instance()
  70.     {
  71.         return *getPointer();
  72.     }

  73.     static void release()
  74.     {
  75.         if(m_instance)
  76.         {
  77.             delete m_instance;
  78.             m_instance = NULL;
  79.         }
  80.     }

  81. private:
  82.     static void* m_instance;
  83.     static CLock m_Lock;
  84. };

  85. template<typename T>
  86. void* Singleton<T>::m_instance = 0;

  87. template<typename T>
  88. CLock Singleton<T>::m_Lock;

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


  90. DWORD WINAPI func(LPVOID p)
  91. {
  92.     
  93.     int i = 0;
  94.     while(i < 5)
  95.     {
  96.         Singleton<TestClass>::instance().Inc();
  97.         Singleton<TestClass>::instance().Show();

  98.         Sleep(1);

  99.         ++i;
  100.     }

  101.     return 1;
  102. }

  103. int main()
  104. {
  105.     for(int i = 0; i < 3; ++i)
  106.     {
  107.         g_hThreads[i] = ::CreateThread(NULL, 0, func, NULL, 0, NULL);
  108.     }

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

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

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

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

  113.     system("pause");

  114.     return 0;
  115. }
程序运行结果
x = 1
x = 2
x = 3
x = 4
x = 5
x = 6
x = 7
x = 9
x = 8
x = 10
x = 12
x = 11
x = 13
x = 14
x = 15
finished
has create 1 TestClass!
阅读(4163) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~