关闭

单例模式具体事例,用到window下多线程,线程锁,模板等功能

标签: 单例模式具体事例
170人阅读 评论(0) 收藏 举报
分类:
  1. 转自:http://blog.chinaunix.net/uid-25808509-id-3026355.html



  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(< 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!

-------------------------------------------------------------------------------------------------
为了解决多线程环境,解决静态对象的删除,采用局部静态对象,得到最优化,最简约的单例模式如下:
#include <QtCore/QCoreApplication>
#include <QDebug>

class Singleton
{
private:
    Singleton() 
    {
        qDebug()<<"Contruct Singleton";
    }
    Singleton(const Singleton&);
    Singleton& operator=(const Singleton&);
 public:
    static Singleton * GetInstance()
    {
       static Singleton m_Single;
       return &m_Single;
    }
};
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    Singleton *tmp = Singleton::GetInstance();
    return a.exec();
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:3471次
    • 积分:64
    • 等级:
    • 排名:千里之外
    • 原创:2篇
    • 转载:7篇
    • 译文:0篇
    • 评论:0条
    文章分类
    文章存档