• <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 };
            這是教科書上使用的方法。看起來(lá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)上面這一切,這讓我覺得煩死了。于是我想到了模板來(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)的方法沒有什么不同。
            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 閱讀(1707) 評(píng)論(2)  編輯 收藏 引用 所屬分類: C++

            評(píng)論

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

            尚未完美,主要是線程安全那塊。2個(gè)問(wèn)題,1,如果不需要線程安全,那塊代碼造成額外的開銷。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)論   

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

            導(dǎo)航

            統(tǒng)計(jì)

            常用鏈接

            留言簿

            隨筆分類

            隨筆檔案

            搜索

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            久久亚洲欧美国产精品| 国产激情久久久久影院小草 | 亚洲精品成人久久久| 久久综合一区二区无码| 亚洲国产成人精品女人久久久 | 久久久国产乱子伦精品作者| 欧美一区二区精品久久| 久久亚洲国产精品成人AV秋霞| 久久亚洲精品无码AV红樱桃| 久久夜色精品国产www| 精品永久久福利一区二区| 久久久WWW免费人成精品| 四虎久久影院| 丰满少妇高潮惨叫久久久| 久久毛片免费看一区二区三区| 嫩草伊人久久精品少妇AV| 无码任你躁久久久久久老妇| 丁香狠狠色婷婷久久综合| 中文精品99久久国产| 中文字幕成人精品久久不卡| 亚洲综合精品香蕉久久网| 思思久久99热免费精品6| 亚洲天堂久久精品| 97久久精品午夜一区二区| 久久天堂AV综合合色蜜桃网| 国产69精品久久久久APP下载| 91精品国产综合久久香蕉| av国内精品久久久久影院| 色婷婷综合久久久久中文一区二区| 午夜肉伦伦影院久久精品免费看国产一区二区三区 | 99精品国产综合久久久久五月天 | 久久婷婷成人综合色综合| 国产99久久久国产精品小说| 久久久黄片| 久久久久无码专区亚洲av| 久久AAAA片一区二区| 国产精品伦理久久久久久| 国产精品美女久久久久av爽| 丰满少妇人妻久久久久久4| 国产一久久香蕉国产线看观看| 精品国产VA久久久久久久冰|