• <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++完美實(shí)現(xiàn)Singleton模式

            Singleton模式是常用的設(shè)計(jì)模式之一,但是要實(shí)現(xiàn)一個(gè)真正實(shí)用的設(shè)計(jì)模式卻也不是件容易的事情。
            標(biāo)準(zhǔn)的實(shí)現(xiàn)
             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 };
            這是教科書(shū)上使用的方法。看起來(lái)沒(méi)有什么問(wèn)題,其實(shí)包含很多的問(wèn)題。下面我們一個(gè)一個(gè)的解決。
            問(wèn)題一  自動(dòng)垃圾回收
            上面的程序必須記住在程序結(jié)束的時(shí)候,釋放內(nèi)存。為了讓它自動(dòng)的釋放內(nèi)存,我們引入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;
            問(wèn)題二  增加模板
            在我的一個(gè)工程中,有多個(gè)的Singleton類,對(duì)Singleton類,我都要實(shí)現(xiàn)上面這一切,這讓我覺(jué)得煩死了。于是我想到了模板來(lái)完成這些重
            復(fù)的工作。
            現(xiàn)在我們要添加本文中最吸引人單件實(shí)現(xiàn):
             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 }
            問(wèn)題三  線程安全
            上面的程序可以適應(yīng)單線程的程序。但是如果把它用到多線程的程序就會(huì)發(fā)生問(wèn)題。主要的問(wèn)題在于同時(shí)執(zhí)行_instance.reset ( new T); 
            就會(huì)同時(shí)產(chǎn)生兩個(gè)新的對(duì)象,然后馬上釋放一個(gè),這跟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 類主要的功能是線程訪問(wèn)同步,代碼如下:
             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 };
            問(wèn)題四  實(shí)用方法
            比如你有一個(gè)需要實(shí)現(xiàn)單件模式的類,就應(yīng)該這樣實(shí)現(xiàn):
             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;
            在使用的時(shí)候很簡(jiǎn)單,跟一般的Singleton實(shí)現(xiàn)的方法沒(méi)有什么不同。
            1 int _tmain(int argc, _TCHAR* argv[])
            2 {
            3         SSManger::instance()->Run();
            4 }
            一個(gè)簡(jiǎn)單的Singleton模式的實(shí)現(xiàn),可以看到C++語(yǔ)言背后隱藏的豐富的語(yǔ)意,我希望有人能實(shí)現(xiàn)一個(gè)更好的Singleton讓大家學(xué)習(xí)。

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

            評(píng)論

            # re: C++完美實(shí)現(xiàn)Singleton模式 2012-11-10 08:42 wingforce

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

            # re: C++完美實(shí)現(xiàn)Singleton模式 2012-11-10 21:44 right

            在復(fù)雜的環(huán)境下,單件很不好用,特別是依賴很多的時(shí)候,單件的初始化是個(gè)大問(wèn)題。  回復(fù)  更多評(píng)論   

            <2025年5月>
            27282930123
            45678910
            11121314151617
            18192021222324
            25262728293031
            1234567

            導(dǎo)航

            統(tǒng)計(jì)

            常用鏈接

            留言簿

            隨筆分類

            隨筆檔案

            搜索

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            婷婷久久久亚洲欧洲日产国码AV | 人妻少妇久久中文字幕一区二区 | 无码精品久久久天天影视| 精品99久久aaa一级毛片| 国产精品一区二区久久| 久久国产成人精品麻豆| 久久99国内精品自在现线| 欧美喷潮久久久XXXXx| 亚洲精品国精品久久99热一| 日本久久久久亚洲中字幕| 久久久老熟女一区二区三区| 久久国产高潮流白浆免费观看| 久久精品亚洲一区二区三区浴池 | 亚洲va久久久噜噜噜久久天堂| 欧美精品福利视频一区二区三区久久久精品 | 久久国产精品99国产精| 亚洲精品国产第一综合99久久| 久久福利青草精品资源站| 久久久久亚洲av无码专区| 99精品久久久久久久婷婷 | 久久96国产精品久久久| 国产精品久久影院| 国产精品美女久久久| 久久免费的精品国产V∧| 色8久久人人97超碰香蕉987| 亚洲综合伊人久久综合| 亚洲AV日韩精品久久久久久久| 性做久久久久久久| 久久久久亚洲AV无码麻豆| 亚洲AV乱码久久精品蜜桃| 亚洲精品无码久久久久| 国产亚洲精品自在久久| 97精品伊人久久久大香线蕉| 久久精品国产亚洲AV麻豆网站 | 久久影视国产亚洲| 国产精品99久久久久久宅男小说| 久久无码高潮喷水| 九九久久自然熟的香蕉图片| 日韩精品国产自在久久现线拍| 久久免费视频6| 色综合久久无码中文字幕|