• <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>
            Creative Commons License
            本Blog采用 知識共享署名-非商業性使用-禁止演繹 3.0 Unported許可協議 進行許可。 —— Fox <游戲人生>

            游戲人生

            游戲人生 != ( 人生 == 游戲 )
            站點遷移至:http://www.yulefox.com。請訂閱本博的朋友將RSS修改為http://feeds.feedburner.com/yulefox
            posts - 62, comments - 508, trackbacks - 0, articles - 7

            ACE vs Boost: Singleton的實現

            Posted on 2009-09-22 00:38 Fox 閱讀(7440) 評論(9)  編輯 收藏 引用 所屬分類: T技術碎語

            本文同步自游戲人生

            以前曾經討論過Singleton的實現,這次在對照ACE和Boost代碼的時候,又重新審視了一下二者對Singleton不同的實現。其間的差別也體現了不同的編程哲學:ACE的實現更加偏重多線程中的安全和效率問題;Boost的實現則偏重于使用語言自身的特性滿足Singleton模式的基本需求。

            o ACE的實現

            Douglas C. Schmidt在Double-Checked Locking: An Optimization Pattern for Efficiently Initializing and Accessing Thread-safe Objects一文中對double-check lock(一般譯為雙檢鎖)進行了詳細的闡述。

            ACE的Singleton使用Adapter模式實現對其他類的適配,使之具有全局唯一的實例。由于C++標準并非明確指定全局靜態對象的初始化順序,ACE使用double-check lock保證線程安全,并使之不受全局靜態對象初始化順序的影響,同時也避免了全局靜態實現方式的初始化后不使用的開銷。

            如果你能夠準確的區分以下三種實現的弊端和隱患,對double-check lock也就有了足夠的了解。

            // -------------------------------------------
            class Singleton
            {
            public:
                static Singleton *instance (void)
                {
                    // Constructor of guard acquires
                    // lock_ automatically.
                    Guard<Mutex> guard (lock_);
                    // Only one thread in the
                    // critical section at a time.
                    if (instance_ == 0)
                        instance_ = new Singleton;
                    return instance_;
                    // Destructor of guard releases
                    // lock_ automatically.
                }
            private:
                static Mutex lock_;
                static Singleton *instance_;
            };

            // ---------------------------------------------
            static Singleton *instance (void)
            {
                if (instance_ == 0) {
                    Guard<Mutex> guard (lock_);
                    // Only come here if instance_
                    // hasn’t been initialized yet.
                    instance_ = new Singleton;
                }
                return instance_;
            }

            // ---------------------------------------------
            class Singleton
            {
            public:
                static Singleton *instance (void)
                {
                    // First check
                    if (instance_ == 0)
                    {
                        // Ensure serialization (guard
                        // constructor acquires lock_).
                        Guard<Mutex> guard (lock_);
                        // Double check.
                        if (instance_ == 0)
                            instance_ = new Singleton;
                    }
                    return instance_;
                    // guard destructor releases lock_.
                }
            private:
                static Mutex lock_;
                static Singleton *instance_;
            };

            更多詳情,見Schmidt老師的原文和ACE_Singleton實現。

            o Boost的實現

            Boost的Singleton也是線程安全的,而且沒有使用鎖機制。當然,Boost的Singleton有以下限制(遵從這些限制,可以提高效率):

            o The classes below support usage of singletons, including use in program startup/shutdown code, AS LONG AS there is only one thread running before main() begins, and only one thread running after main() exits.

            o This class is also limited in that it can only provide singleton usage for classes with default constructors.

            // T must be: no-throw default constructible and no-throw destructible
            template <typename T>
            struct singleton_default
            {
            private:
                struct object_creator
                {
                    // This constructor does nothing more than ensure that instance()
                    //  is called before main() begins, thus creating the static
                    //  T object before multithreading race issues can come up.
                    object_creator() { singleton_default<T>::instance(); }
                    inline void do_nothing() const { }
                };
                static object_creator create_object;

                singleton_default();

            public:
                typedef T object_type;

                // If, at any point (in user code), singleton_default<T>::instance()
                //  is called, then the following function is instantiated.
                static object_type & instance()
                {
                    // This is the object that we return a reference to.
                    // It is guaranteed to be created before main() begins because of
                    //  the next line.
                  static object_type obj;

                  // The following line does nothing else than force the instantiation
                  //  of singleton_default<T>::create_object, whose constructor is
                  //  called before main() begins.
                  create_object.do_nothing();

                  return obj;
                }
            };
            template <typename T>
            typename singleton_default<T>::object_creator
            singleton_default<T>::create_object;

            對于多數Singleton使用,Boost提供的版本完全能夠滿足需求。為了效率,我們有必要對其使用作出一定的限制。

            而在多線程編程中,則有必要使用double-check lock降低頻繁加鎖帶來的開銷。

            -------------------------------------------------------------------------------

            PS: 欣賞Soft的一句話:經得起誘惑,耐得住寂寞

            Feedback

            # re: ACE vs Boost: Singleton的實現  回復  更多評論   

            2009-09-22 02:44 by OwnWaterloo
            boost夠機靈的, 避重就輕。


            singleton可能會遇到的問題, 以及如何解決, 最詳盡的資料在《modern c++ design》中有介紹。 問題大致有如下幾個方面:
            1. 限制實例數量
            2. singleton相互引用
            3. dead-reference
            4. 線程安全


            C++中:
            singleton這種模式能"直接"解決的問題只有1;利用C++語言機制 —— private —— 來限制實例數量。
            而問題1也是眾多文章討論得最多的, 簡單嘛……

            解決1問題,并不一定需要singlet這種模式;而其他問題又不是singleton這種"模式"本身能解決的。
            綜上, singleton in cplusplus is bullshit ……



            boost這種實現,只能解決1, 在一定條件下,能解決4。
            如果遇見2、3, 同樣完蛋。

            boost.singleton解決4,需要滿足如下條件:
            "The classes below support usage of singletons, including use in program startup/shutdown code, AS LONG AS there is only one thread running before main() begins, and only one thread running after main() exits."
            如果這種條件能被滿足, 直接使用全局變量同樣是線程安全的。

            如果真的需要解決問題1,以及難看的全局變量:
            可以將這個全局變量以及類的定義放在單一翻譯單元中,并在該翻譯但與實現若干wrapper函數即可。

            同樣, 對于問題2和3, 全局變量也通常會壞事。

            綜上, boost.singleton, 簡直就是為模式而模式。

            # re: ACE vs Boost: Singleton的實現  回復  更多評論   

            2009-09-22 11:07 by 陳梓瀚(vczh)
            @OwnWaterloo
            一個程序應該滿足下面的條件:
            將程序里面所有的全局變量、函數和類的暴露對象排序(只需存在一種排序即可),對于所有的0<=n<=這些東西的數量,刪除前n個,程序應當可以編譯。

            # re: ACE vs Boost: Singleton的實現  回復  更多評論   

            2009-09-22 14:29 by OwnWaterloo
            @陳梓瀚(vczh)
            沒明白。 這個條件能保證什么? 編譯時的依賴關系?

            # re: ACE vs Boost: Singleton的實現  回復  更多評論   

            2009-09-22 17:32 by 陳梓瀚(vczh)
            @OwnWaterloo
            便于你的代碼以后復用,久而久之組織成類庫。

            # re: ACE vs Boost: Singleton的實現  回復  更多評論   

            2009-09-22 17:44 by OwnWaterloo
            @陳梓瀚(vczh)
            這跟singleton有什么關系?

            # re: ACE vs Boost: Singleton的實現  回復  更多評論   

            2009-09-24 17:45 by 凡客誠品
            便于你的代碼以后復用,久而久之組織成類庫

            # re: ACE vs Boost: Singleton的實現  回復  更多評論   

            2009-09-28 21:59 by adah
            在double-checked locking pattern已經被批駁了這么久之后還寫這樣的文章,作者也可真稱得上孤陋寡聞了。

            # re: ACE vs Boost: Singleton的實現  回復  更多評論   

            2009-09-29 09:19 by Fox
            @adah
            給個鏈接我了解一下DCL被批的原因吧,我承認孤陋寡聞了。

            # re: ACE vs Boost: Singleton的實現  回復  更多評論   

            2009-12-03 16:06 by 金慶
            以上DCL單件的實現可能是有缺陷。但我不是很確信。因為ACE確實是這樣實現的。按我的理解,加個volatile就好了。“The "Double-Checked Locking is Broken" Declaration”( http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html )一文可作參考。不知adah所說的批駁是否指這一點?
            少妇久久久久久被弄到高潮| 国产精品美女久久久久久2018| 久久这里只有精品首页| 国产福利电影一区二区三区久久久久成人精品综合 | 久久久久亚洲av成人无码电影| 久久人人爽人人爽人人片AV麻豆| 中文字幕精品久久久久人妻| 久久精品蜜芽亚洲国产AV| 99久久精品免费看国产| 久久久久久国产精品无码下载| 99久久精品国产高清一区二区 | 久久国产亚洲精品| 久久亚洲精品中文字幕三区| 精品久久久久成人码免费动漫| 狠狠狠色丁香婷婷综合久久五月 | 一本大道久久a久久精品综合| 久久天天躁狠狠躁夜夜2020一| 久久亚洲精品中文字幕三区| 久久婷婷五月综合成人D啪| 66精品综合久久久久久久| 亚洲女久久久噜噜噜熟女| 久久久精品国产亚洲成人满18免费网站 | 日本WV一本一道久久香蕉| 国产免费福利体检区久久| 国产精品久久久久久影院| 久久久久精品国产亚洲AV无码| 亚洲精品99久久久久中文字幕| 99国内精品久久久久久久| 九九99精品久久久久久| 人妻无码中文久久久久专区| 国产激情久久久久久熟女老人| 婷婷久久综合| 香蕉99久久国产综合精品宅男自 | 日韩欧美亚洲综合久久影院d3| 久久精品欧美日韩精品| 久久综合精品国产二区无码| 久久综合88熟人妻| 久久夜色精品国产亚洲| 精品多毛少妇人妻AV免费久久| 久久99久久成人免费播放| 国产精品99久久久久久宅男|