模板singleton模式的C++實現

來源:互聯網
上載者:User

標籤:http   執行個體   private   blog   eve   模板類   library   127.0.0.1   res   

模板singleton模式的C++實現

  近期回過頭整理了一下singleton模式,看了別人寫的關於singleton的介紹。發現這個singleton模式雖然簡單,但要寫一個穩定/安全執行緒/泛型的模板singleton,還是需要一點技巧的。經過整理,要寫一個模板的singlton至少需要以下幾個知識點:

1:singleton

2:static類成員執行個體延遲建立。

3:static類執行個體建立互斥實現,即安全執行緒。

4:double check技巧。  【Ref:http://www.ibm.com/developerworks/library/j-dcl/index.html】

5:模板。

6:static成員變數初始化。

7:singleton關閉類執行個體

下面上singleton實現代碼,測試可用。

 1 #pragma once 2 /************************************************************************ 3 singleton模式類模板 4 1:延遲建立類執行個體    2:double check    3:互斥訪問    4:模板 5 ************************************************************************/ 6 #ifndef WHG_SINGLETON 7 #define WHG_SINGLETON 8  9 #include "ThreadLockCs.h"10 11 template<class T>12 class CSingleton13 {14 private:15     static T* _instance;16     CSingleton(void);17     static CThreadLockCs lcs;18 public:19     static T* Instance(void);20     static void Close(void);21 };22 23 //模板類static變數24 template<class T> 25 T*  CSingleton<T>::_instance = NULL;26 27 template<class T> 28 CThreadLockCs CSingleton<T>::lcs;29 30 //模板類方法實現31 template<class T>32 CSingleton<T>::CSingleton(void)33 {    34 }35 36 template<class T>37 T*  CSingleton<T>::Instance(void)38 {39     //double-check40     //延遲建立,只有調用方訪問Instance才會建立類執行個體41     if (_instance == NULL)42     {43         //互斥訪問鎖,用CriticalSection實現44         lcs.lock();45         if (_instance == NULL)46         {47             _instance = new T;48         }49         lcs.unlock();50     }51     return _instance;52 }53 54 template<class T>55 void CSingleton<T>::Close(void)56 {57     if (_instance)58     {59         delete _instance;60     }61 }62 63 #endif

 

CSingleton內用到的CThreadLockCs 實現如下:

 1 //互斥訪問鎖 2 #ifndef WHG_THREAD_LOCKCS 3 #define WHG_THREAD_LOCKCS 4  5 //#include <Windows.h> 6  7 class CThreadLockCs 8 { 9 public:10     CThreadLockCs(){InitializeCriticalSection(&m_cs);}11     ~CThreadLockCs(){DeleteCriticalSection(&m_cs);}12     void lock(){EnterCriticalSection(&m_cs);}13     void unlock(){LeaveCriticalSection(&m_cs);}14 private:15     CRITICAL_SECTION m_cs;16 };17 #endif

 

調用方法很簡單:

1:用typedef申明一個變數名

1 typedef CSingleton<CListen>    LISTEN;

 

2:調用的地方寫

1 LISTEN::Instance()->SetAddress(8828,"127.0.0.1");

 

3:關閉的地方寫

1 LISTEN::Close();

 

模板singleton模式的C++實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.