• <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>

            #ant

            The dreams in which I'm dying are the best I've ever had...

            非完美C++ Singleton實現(xiàn)[1]

            Singleton模式是一種非常簡單的設(shè)計模式,這種模式很常用也很容易被濫用。當(dāng)你設(shè)計應(yīng)用程序的時候,經(jīng)常會遇到某些對象在整個程序的生命周期應(yīng)該僅有一個實例的情況,比如File System,Graphic System,Logging Utility,這時候就可以用到Singleton模式。Singleton模式在GOF中描述如下:

                    Ensure a class only has one instance, and provide a global point of access to it.

            Singleton模式的定義很簡單,實現(xiàn)也有N多種,但是卻很難找到一個稱得上“完美”的。實現(xiàn)一個完美的Singleton比想象中要難的多,下面探索性的來實現(xiàn)一個非完美的。

            1.典型實現(xiàn)
            在C++中,Singleton模式的典型實現(xiàn)如下:

             1 // Header file Singleton.h
             2 class  Singleton {
             3 public
            :
             4     static Singleton& Instance() { // Unique point of access

             5         if (0 ==  _instance)
             6             _instance = new
             Singleton();
             7         return *
            _instance;
             8 
                }
             9     void
             DoSomething();
            10 private
            :
            11     Singleton(); // Prevent clients from creating a new Singleton

            12     ~Singleton(); // Prevent clients from deleting a Singleton
            13     Singleton(const Singleton&); // Prevent clients from copying a Singleton
            14     Singleton& operator=(const Singleton& );
            15 private
            :
            16     static Singleton *_instance; // The one and only instance

            17  };
            18 

            19 // Implementation file Singleton.cpp
            20 Singleton* Singleton::_instance = 0;


            通過將Singleton的構(gòu)造函數(shù)設(shè)為private可以禁止客戶代碼直接創(chuàng)建Singleton對象,除此之外,Singleton的copy constructor和copy assignment operator都為private且僅有聲明沒有實現(xiàn),禁止了客戶代碼拷貝Singleton對象。唯一可以創(chuàng)建Singleton對象的是Singleton自己的靜態(tài)成員函數(shù)Instance,這樣就在編譯器保證了Singleton實例的唯一性。上面這些是在C++中實現(xiàn)Singleton模式最基本的要點。

            Instance方法保證只有在第一次調(diào)用時才會生成Singleton對象,以后的調(diào)用只是簡單返回唯一的已存在的實例。Instance方法實際上實現(xiàn)的是懶惰初始化(lazy initialize),如果程序中根本沒有用到Singleton對象,也就根本不會產(chǎn)生Singleton的實例,這在Singleton對象很少使用且創(chuàng)建Singleton對象開銷比較大的情況下特別有用。

            客戶代碼現(xiàn)在可以這樣使用Singleton:

            1 Singleton &=  Singleton::Instance();
            2 s.DoSomething();


            還需要說明的是Singleton的析構(gòu)函數(shù),析構(gòu)函數(shù)也為private可以禁止客戶寫出如下代碼。如果某個客戶寫出了如下代碼,隨后的對Singleton的訪問就會導(dǎo)致為定義行為,因為Singleton對象已經(jīng)不存在。

            1 Singleton *= & Singleton::Instance();
            2 delete p;


            2.引入smart pointer
            上面的實現(xiàn)算是一個好的實現(xiàn)嗎?當(dāng)然不是,或許連一個正確的實現(xiàn)都算不上。如果你想湊合,當(dāng)然沒問題,上面的代碼大多數(shù)情況下可以工作的很好。也許你已經(jīng)注意到了一些問題,比如說在上面的代碼中只有new沒有delete。是的,你說會發(fā)生memory leak對吧,其實memory leak都不是主要的問題,所有的現(xiàn)代操作系統(tǒng)在進(jìn)程結(jié)束的時候都會對內(nèi)存很好的進(jìn)行回收。比memory leak更值得讓人擔(dān)憂的是resource leak,如果Singleton在構(gòu)造函數(shù)中請求了某些資源:網(wǎng)絡(luò)連接,文件句柄,數(shù)據(jù)庫連接等。這些資源將得不到釋放。

            唯一修正resource leak的方法就是在程序結(jié)束的時候delete _instance。當(dāng)然了,用smart pointer再好不過,在這里用auto_ptr就可以滿足需要了(如果你還不知道smart_ptr是什么,花點時間熟悉C++標(biāo)準(zhǔn)庫吧),修改后的代碼如下:

             1 // Header file Singleton.h
             2 class  Singleton {
             3 public
            :
             4     static Singleton& Instance() { // Unique point of access

             5         if (0 ==  _instance.get())
             6             _instance.reset(new
             Singleton());
             7         return *
            (_instance.get());
             8 
                }
             9     void
             DoSomething(){}
            10 private
            :
            11     Singleton(){} // Prevent clients from creating a new Singleton

            12     ~Singleton(){} // Prevent clients from deleting a Singleton
            13     Singleton(const Singleton&); // Prevent clients from copying a Singleton
            14     Singleton& operator=(const Singleton& );
            15 private
            :
            16     friend auto_ptr<Singleton>
            ;
            17     static auto_ptr<Singleton> _instance; // The one and only instance

            18  };
            19 

            20 // Implementation file Singleton.cpp
            21 auto_ptr<Singleton> Singleton::_instance;


            3.用atexit替換smart pointer
            C++并沒有規(guī)定不同編譯單元(translation unit,簡單說就是一個可編譯的cpp文件)中static對象的初始化順序。如果一個程序中有多個Singleton對象,那么這些Singleton對象的析構(gòu)順序也將是任意的。很顯然,當(dāng)多個Singleton對象有依賴關(guān)系時,smart pointer根本無法保證Singleton的析構(gòu)順序。

            msdn中對atexit描述如下:

            The atexit function is passed the address of a function (func) to be called when the program terminates normally. Successive calls to atexit create a register of functions that are executed in last-in, first-out (LIFO) order. The functions passed to atexit cannot take parameters. atexit  use the heap to hold the register of functions. Thus, the number of functions that can be registered is limited only by heap memory.

            需要說明的是atexit并不比smart pointer好多少,LIFO的保證對于有復(fù)雜依賴關(guān)系的多個Singleton依然束手無力,但是用atexit替換smart pointer卻是必須的,它是設(shè)計完美Singleton的基礎(chǔ)。


            #如果你疑惑atexit為什么還是不行,請考慮下面的情況:
            NOTE:下面的情況在Modern C++ Design中叫做KDL(Keyboard,Display,Log)problem

            某個程序中使用了如下3個Singleton:KeyboardDisplayLogKeyboardDisplay分別對應(yīng)于計算機的鍵盤和顯示器,Log用來記錄錯誤信息。假設(shè)當(dāng)KeyboardDisplay的構(gòu)造函數(shù)和析構(gòu)函數(shù)出現(xiàn)錯誤時會調(diào)用Log記錄錯誤信息,并且構(gòu)造和析構(gòu)導(dǎo)致的任何錯誤都會終止程序。

            在程序啟動時,如果Keyboard構(gòu)造成功,Display構(gòu)造失敗,很顯然在Display的構(gòu)造函數(shù)中將會構(gòu)造Log而且失敗信息會被Log記錄,根據(jù)假設(shè)這時候程序準(zhǔn)備退出,atexit注冊的函數(shù)將會按LIFO的順序被調(diào)用。因為Keyboard先于Log構(gòu)造,所以Log先于Keyboard析構(gòu),但是當(dāng)由于某種原因Keyboard在析構(gòu)時失敗,想要調(diào)用Log記錄錯誤信息時,Log早已被銷毀,則Log::Instance()將會導(dǎo)致未定義行為。

            #atexit的嚴(yán)重問題:
            從上面的例子可以看出,atexit和smart pointer相比僅僅是有LIFO的保證而已,這樣的保證貌似也不怎么有效,因為atexit跟smart pointer一樣也無法解決KDL probleam

            atexit由于LIFO帶來了另外的問題,看下面的代碼:

             1 #include <cstdlib>
             2 void  Bar() {
             3 
                ...
             4 
            }
             5 void
             Foo() {
             6 
                std::atexit(Bar);
             7 
            }
             8 int
             main() {
             9 
                std::atexit(Foo);
            10     return 0
            ;
            11 }

            上面的小段代碼用atexit注冊了Foo,F(xiàn)oo調(diào)用了std::atexit(Bar)。當(dāng)程序退出時,根據(jù)atexit的LIFO保證,Bar在Foo之后注冊,因此Bar應(yīng)該在Foo之前調(diào)用,但是當(dāng)Bar注冊的時候Foo已經(jīng)調(diào)用了,Bar根本就沒有機會能夠在Foo之前調(diào)用。這明顯自相矛盾對吧,沒辦法,C++標(biāo)準(zhǔn)好像忽視了這一點,因此如果類似代碼被調(diào)用,肯定不會有什么好的結(jié)果,好一點是resource leak,差一點估計程序就崩潰了!!!

            atexit的這個問題跟Singleton有關(guān)系嗎?當(dāng)然有,如果在一個Singleton的析構(gòu)函數(shù)中調(diào)用atexit就會出現(xiàn)上述問題。即在KDL problem中,如果KeyboardDisplay都構(gòu)造成功,當(dāng)KeyboardDisplay任意一個析構(gòu)失敗時,KeyboardDisplay在析構(gòu)函數(shù)中會構(gòu)造LogLog的構(gòu)造函數(shù)會間接調(diào)用atexit。oops!!!,可怕的未定義行為。

            看到這里你一定對atexit相當(dāng)失望,貌似它帶來的好處多于壞處。但是請你相信,如果適當(dāng)設(shè)計,atexit在后面的Singleton改造中會起到很重要的作用。

            用atexit后的代碼:

             1 // Header file Singleton.h
             2 class  Singleton {
             3 public
            :
             4     static Singleton& Instance() { // Unique point of access

             5         if (0 ==  _instance) {
             6             _instance = new
             Singleton();
             7             atexit(Destroy); // Register Destroy function

             8          }
             9         return *
            _instance;
            10 
                }
            11     void
             DoSomething(){}
            12 private
            :
            13     static void Destroy() { // Destroy the only instance

            14         if ( _instance != 0  ) {
            15 
                        delete _instance;
            16             _instance = 0
            ;
            17 
                    }
            18 
                }
            19     Singleton(){} // Prevent clients from creating a new Singleton

            20     ~Singleton(){} // Prevent clients from deleting a Singleton
            21     Singleton(const Singleton&); // Prevent clients from copying a Singleton
            22     Singleton& operator=(const Singleton& );
            23 private
            :
            24     static Singleton *_instance; // The one and only instance

            25  };
            26 

            27 // Implementation file Singleton.cpp
            28 Singleton* Singleton::_instance = 0;

            你有沒有仔細(xì)考慮過Destroy中的_instance = 0;這一行代碼,上述代碼實際上實現(xiàn)的是不死鳥模式(The Phoenix Singleton),所謂不死鳥,就跟一輝一樣可以死而復(fù)生。上面的代碼可以解決本文最早提出的KDL problem,即如果Keyboard析構(gòu)失敗,雖然Log已經(jīng)析構(gòu),但是由于Destroy中的_instance = 0;這一行代碼,Log::Instance()將會創(chuàng)建一個新的Log對象,程序?qū)憩F(xiàn)良好。當(dāng)然了,Phoenix Singleton僅能用于無狀態(tài)的Singleton,如果Log需要保存某些狀態(tài),Phoenix Singleton也不會帶來任何好處。你當(dāng)然可以用某些方法維持Phoenix Singleton的狀態(tài),但是在做之前先想想看是否值得,維持狀態(tài)可能會使Singleton變得特別復(fù)雜。

            上面的Phoenix Singleton已經(jīng)可以滿足大部分需要,如果你的Singleton沒有涉及到多線程,多個Singleton之間也沒有依賴關(guān)系,你大可以放心使用。但是如果你用到多線程,或者你的Singleton關(guān)系如KDL般復(fù)雜,或者你覺得對每一個Singleton都敲同樣的代碼讓你厭煩。在后面幾篇會有一個多線程安全的,能夠解決多個Singleton依賴關(guān)系的,基于模板的Singleton實現(xiàn)。

            posted on 2007-09-07 14:49 螞蟻終結(jié)者 閱讀(5013) 評論(12)  編輯 收藏 引用 所屬分類: Design Pattern

            Feedback

            # re: 非完美C++ Singleton實現(xiàn)[1] 2007-09-07 15:04 5Element

            不錯,期待下一篇。  回復(fù)  更多評論   

            # re: 非完美C++ Singleton實現(xiàn)[1] 2007-09-07 15:53 DeathKnight

            先贊一下你的文字排版 很漂亮  回復(fù)  更多評論   

            # re: 非完美C++ Singleton實現(xiàn)[1][未登錄] 2007-09-07 16:22 L

            這個Singleton沒有加鎖,多線程下會有問題。  回復(fù)  更多評論   

            # re: 非完美C++ Singleton實現(xiàn)[1] 2007-09-07 16:36 螞蟻終結(jié)者

            @L
            恩,其實還有很多問題,后面幾篇會解決  回復(fù)  更多評論   

            # re: 非完美C++ Singleton實現(xiàn)[1] 2007-09-07 17:32 bgate

            最起碼應(yīng)該寫成個模板類吧.  回復(fù)  更多評論   

            # re: 非完美C++ Singleton實現(xiàn)[1] 2007-09-07 17:36 Minidx全文檢索

            呵呵,的確是“非完美”的,不過支持一下~~~  回復(fù)  更多評論   

            # re: 非完美C++ Singleton實現(xiàn)[1] 2007-09-07 19:29 螞蟻終結(jié)者

            大家都沒有看到我最后一句話:
            在后面幾篇會有一個多線程安全的,能夠解決多個Singleton依賴關(guān)系的,基于模板的Singleton實現(xiàn)。

            由于篇幅比較長,所以分成好幾篇了,thanks!  回復(fù)  更多評論   

            # re: 非完美C++ Singleton實現(xiàn)[1][未登錄] 2007-09-07 20:49 楊粼波

            看看Loki里面的實現(xiàn)。  回復(fù)  更多評論   

            # re: 非完美C++ Singleton實現(xiàn)[1] 2007-09-08 09:10 螞蟻終結(jié)者

            @楊粼波
            Loki的我看過,不過感覺太復(fù)雜,也許有時候需要的只是最簡單的  回復(fù)  更多評論   

            # re: 非完美C++ Singleton實現(xiàn)[1] 2007-09-09 00:12 攀升

            不知道能不能寫一個原型模式的文章,我最近想用  回復(fù)  更多評論   

            # re: 非完美C++ Singleton實現(xiàn)[1] 2007-09-09 09:30 螞蟻終結(jié)者

            @攀升
            “原型模式”目前還沒有研究過,如果最近有時間的話可能會寫一篇  回復(fù)  更多評論   

            # re: 非完美C++ Singleton實現(xiàn)[1] 2007-09-10 09:45 Uranus

            謝謝,  回復(fù)  更多評論   

            久久97精品久久久久久久不卡| 亚洲欧洲精品成人久久奇米网| 久久精品二区| 香港aa三级久久三级| 精品久久8x国产免费观看| 国产毛片欧美毛片久久久| 四虎国产精品成人免费久久| 久久综合九色欧美综合狠狠| 久久国产福利免费| 久久久久久噜噜精品免费直播| 久久精品人妻一区二区三区| 久久91这里精品国产2020| 91精品国产91久久久久久蜜臀| 一本大道久久a久久精品综合| 成人资源影音先锋久久资源网| 久久久久人妻精品一区二区三区| 人妻精品久久久久中文字幕一冢本| 亚洲熟妇无码另类久久久| 色综合久久久久久久久五月| A狠狠久久蜜臀婷色中文网| 色综合久久中文综合网| 久久久久国产一区二区三区| 久久久久久国产精品无码下载| 久久婷婷人人澡人人爽人人爱| 久久亚洲美女精品国产精品| 99久久精品午夜一区二区| 国产成人久久激情91| 久久婷婷色综合一区二区| 久久青青草视频| 精品久久久无码人妻中文字幕豆芽| 88久久精品无码一区二区毛片| 久久久久无码专区亚洲av| 久久综合九色综合网站| 久久精品aⅴ无码中文字字幕不卡| 久久精品国产91久久麻豆自制 | 久久精品国产色蜜蜜麻豆| 无码国内精品久久综合88| 精品无码久久久久久尤物| 色婷婷综合久久久久中文字幕| 久久人人爽人人爽人人片av高请| 国产无套内射久久久国产|