• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            C++完美實現Singleton模式

            Singleton模式是常用的設計模式之一,但是要實現一個真正實用的設計模式卻也不是件容易的事情。
            標準的實現
             1 class Singleton
             2 {
             3 public:
             4        static Singleton * Instance()
             5        {
             6               if0== _instance)
             7               {
             8                      _instance = new Singleton;
             9               }
            10               return _instance;
            11        }
            12 protected:
            13        Singleton(void)
            14        {
            15        }
            16        virtual ~Singleton(void)
            17        {
            18        }
            19        static Singleton* _instance;
            20 };
            這是教科書上使用的方法。看起來沒有什么問題,其實包含很多的問題。下面我們一個一個的解決。
            問題一  自動垃圾回收
            上面的程序必須記住在程序結束的時候,釋放內存。為了讓它自動的釋放內存,我們引入auto_ptr改變它。
             1 #include <memory>
             2 #include <iostream>
             3 using namespace std;
             4 class Singleton
             5 {
             6 public:
             7        static Singleton * Instance()
             8        {
             9               if0== _instance.get())
            10               {
            11                      _instance.reset( new Singleton);
            12               }
            13               return _instance.get();
            14        }
            15 protected:
            16        Singleton(void)
            17        {
            18               cout <<"Create Singleton"<<endl;
            19        }
            20        virtual ~Singleton(void)
            21        {
            22               cout << "Destroy Singleton"<<endl;
            23        }
            24        friend class auto_ptr<Singleton>;
            25        static auto_ptr<Singleton> _instance;
            26 };
            27 //Singleton.cpp
            28 auto_ptr<Singleton> Singleton::_instance;
            問題二  增加模板
            在我的一個工程中,有多個的Singleton類,對Singleton類,我都要實現上面這一切,這讓我覺得煩死了。于是我想到了模板來完成這些重
            復的工作。
            現在我們要添加本文中最吸引人單件實現:
             1 #include <memory>
             2 using namespace std;
             3 using namespace C2217::Win32;
             4  
             5 namespace C2217
             6 {
             7 namespace Pattern
             8 {
             9 template <class T>
            10 class Singleton
            11 {
            12 public:
            13        static inline T* instance();
            14       
            15 private:
            16        Singleton(void){}
            17        ~Singleton(void){}
            18        Singleton(const Singleton&){}
            19        Singleton & operator= (const Singleton &){}
            20  
            21        static auto_ptr<T> _instance;
            22 };
            23  
            24 template <class T>
            25 auto_ptr<T> Singleton<T>::_instance;
            26  
            27 template <class T>
            28  inline T* Singleton<T>::instance()
            29 {
            30        if0== _instance.get())
            31        {
            32               _instance.reset ( new T);
            33        }
            34       
            35        return _instance.get();
            36 }
            37  
            38 //Class that will implement the singleton mode,
            39 //must use the macro in it's delare file
            40 #define DECLARE_SINGLETON_CLASS( type ) \
            41        friend class auto_ptr< type >;\
            42        friend class Singleton< type >;
            43 }
            44 }
            問題三  線程安全
            上面的程序可以適應單線程的程序。但是如果把它用到多線程的程序就會發生問題。主要的問題在于同時執行_instance.reset ( new T); 
            就會同時產生兩個新的對象,然后馬上釋放一個,這跟Singleton模式的本意不符。所以,你需要更加安全的版本:
             1 #include <memory>
             2 using namespace std;
             3 #include "Interlocked.h"
             4 using namespace C2217::Win32;
             5  
             6 namespace C2217
             7 {
             8 namespace Pattern
             9 {
            10 template <class T>
            11 class Singleton
            12 {
            13 public:
            14        static inline T* instance();
            15       
            16 private:
            17        Singleton(void){}
            18        ~Singleton(void){}
            19        Singleton(const Singleton&){}
            20        Singleton & operator= (const Singleton &){}
            21  
            22        static auto_ptr<T> _instance;
            23        static CResGuard _rs;
            24 };
            25  
            26 template <class T>
            27 auto_ptr<T> Singleton<T>::_instance;
            28  
            29 template <class T>
            30 CResGuard Singleton<T>::_rs;
            31  
            32 template <class T>
            33  inline T* Singleton<T>::instance()
            34 {
            35        if0 == _instance.get() )
            36        {
            37               CResGuard::CGuard gd(_rs);
            38               if0== _instance.get())
            39               {
            40                      _instance.reset ( new T);
            41               }
            42        }
            43        return _instance.get();
            44 }
            45  
            46 //Class that will implement the singleton mode,
            47 //must use the macro in it's delare file
            48 #define DECLARE_SINGLETON_CLASS( type ) \
            49        friend class auto_ptr< type >;\
            50        friend class Singleton< type >;
            51 }
            52 }
            CresGuard 類主要的功能是線程訪問同步,代碼如下:
             1 class CResGuard {
             2 public:
             3    CResGuard()  { m_lGrdCnt = 0; InitializeCriticalSection(&m_cs); }
             4    ~CResGuard() { DeleteCriticalSection(&m_cs); }
             5  
             6    // IsGuarded is used for debugging
             7    BOOL IsGuarded() const { return(m_lGrdCnt > 0); }
             8  
             9 public:
            10    class CGuard {
            11    public:
            12       CGuard(CResGuard& rg) : m_rg(rg) { m_rg.Guard(); };
            13       ~CGuard() { m_rg.Unguard(); }
            14  
            15    private:
            16       CResGuard& m_rg;
            17    };
            18  
            19 private:
            20    void Guard()   { EnterCriticalSection(&m_cs); m_lGrdCnt++; }
            21    void Unguard() { m_lGrdCnt--; LeaveCriticalSection(&m_cs); }
            22  
            23    // Guard/Unguard can only be accessed by the nested CGuard class.
            24    friend class CResGuard::CGuard;
            25  
            26 private:
            27    CRITICAL_SECTION m_cs;
            28    long m_lGrdCnt;   // # of EnterCriticalSection calls
            29 };
            問題四  實用方法
            比如你有一個需要實現單件模式的類,就應該這樣實現:
             1 #include "singleton.h"
             2 using namespace C2217::Pattern;
             3  
             4 class ServiceManger
             5 {
             6 public:
             7        void Run()
             8        {
             9        }
            10 private:
            11        ServiceManger(void)
            12        {
            13        }
            14        virtual ~ServiceManger(void)
            15        {
            16        }
            17        DECLARE_SINGLETON_CLASS(ServiceManger);
            18 };
            19  
            20 typedef Singleton<ServiceManger> SSManger;
            在使用的時候很簡單,跟一般的Singleton實現的方法沒有什么不同。
            1 int _tmain(int argc, _TCHAR* argv[])
            2 {
            3         SSManger::instance()->Run();
            4 }
            一個簡單的Singleton模式的實現,可以看到C++語言背后隱藏的豐富的語意,我希望有人能實現一個更好的Singleton讓大家學習。

            posted on 2012-11-09 14:10 Beatles 閱讀(1707) 評論(2)  編輯 收藏 引用 所屬分類: C++

            評論

            # re: C++完美實現Singleton模式 2012-11-10 08:42 wingforce

            尚未完美,主要是線程安全那塊。2個問題,1,如果不需要線程安全,那塊代碼造成額外的開銷。2,臨界區的效率依然比較差。解決方法,1,引入額外的模板參數,multi_thread, single_thread,然后template <class T, class thread_mode = multi_thread> class Singleton {}; 2, 使用Lock Free技術  回復  更多評論   

            # re: C++完美實現Singleton模式 2012-11-10 21:44 right

            在復雜的環境下,單件很不好用,特別是依賴很多的時候,單件的初始化是個大問題。  回復  更多評論   

            <2012年11月>
            28293031123
            45678910
            11121314151617
            18192021222324
            2526272829301
            2345678

            導航

            統計

            常用鏈接

            留言簿

            隨筆分類

            隨筆檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            伊人久久综在合线亚洲2019| 欧美精品九九99久久在观看| MM131亚洲国产美女久久| 久久精品中文字幕无码绿巨人| 久久不见久久见免费视频7| 久久精品国产99久久无毒不卡| 久久精品国产99国产电影网| 国产综合成人久久大片91| 性高湖久久久久久久久AAAAA| yy6080久久| 久久电影网2021| 性做久久久久久免费观看| 狠狠综合久久AV一区二区三区| 99久久人妻无码精品系列蜜桃 | 久久人人爽人人精品视频| 97精品伊人久久大香线蕉| 91精品国产91久久综合| 亚洲人成网站999久久久综合 | 久久精品无码av| 久久精品国产99国产精品亚洲 | 亚洲欧美国产日韩综合久久| 无码日韩人妻精品久久蜜桃 | 日韩精品国产自在久久现线拍| 亚洲精品成人网久久久久久| 亚洲AV无码成人网站久久精品大| 97超级碰碰碰碰久久久久| 欧美日韩精品久久免费| 国产精品99久久久久久宅男| 亚洲国产精品久久久天堂| 久久人人爽人人爽人人片AV东京热| 久久精品国产99久久久 | 国产精品gz久久久| 久久综合精品国产二区无码| 久久伊人色| 国产成人精品久久综合 | 久久久无码精品亚洲日韩蜜臀浪潮| 国产午夜精品理论片久久影视 | 91精品国产91久久久久久青草| 久久久久久亚洲精品影院| 久久e热在这里只有国产中文精品99| 久久精品中文騷妇女内射|