• <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采用 知識(shí)共享署名-非商業(yè)性使用-禁止演繹 3.0 Unported許可協(xié)議 進(jìn)行許可。 —— Fox <游戲人生>

            游戲人生

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

            ACE vs Boost: Singleton的實(shí)現(xiàn)

            Posted on 2009-09-22 00:38 Fox 閱讀(7439) 評(píng)論(9)  編輯 收藏 引用 所屬分類(lèi): T技術(shù)碎語(yǔ)

            本文同步自游戲人生

            以前曾經(jīng)討論過(guò)Singleton的實(shí)現(xiàn),這次在對(duì)照ACE和Boost代碼的時(shí)候,又重新審視了一下二者對(duì)Singleton不同的實(shí)現(xiàn)。其間的差別也體現(xiàn)了不同的編程哲學(xué):ACE的實(shí)現(xiàn)更加偏重多線程中的安全和效率問(wèn)題;Boost的實(shí)現(xiàn)則偏重于使用語(yǔ)言自身的特性滿足Singleton模式的基本需求。

            o ACE的實(shí)現(xiàn)

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

            ACE的Singleton使用Adapter模式實(shí)現(xiàn)對(duì)其他類(lèi)的適配,使之具有全局唯一的實(shí)例。由于C++標(biāo)準(zhǔn)并非明確指定全局靜態(tài)對(duì)象的初始化順序,ACE使用double-check lock保證線程安全,并使之不受全局靜態(tài)對(duì)象初始化順序的影響,同時(shí)也避免了全局靜態(tài)實(shí)現(xiàn)方式的初始化后不使用的開(kāi)銷(xiāo)。

            如果你能夠準(zhǔn)確的區(qū)分以下三種實(shí)現(xiàn)的弊端和隱患,對(duì)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_;
            };

            更多詳情,見(jiàn)Schmidt老師的原文和ACE_Singleton實(shí)現(xiàn)。

            o Boost的實(shí)現(xiàn)

            Boost的Singleton也是線程安全的,而且沒(méi)有使用鎖機(jī)制。當(dāng)然,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;

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

            而在多線程編程中,則有必要使用double-check lock降低頻繁加鎖帶來(lái)的開(kāi)銷(xiāo)。

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

            PS: 欣賞Soft的一句話:經(jīng)得起誘惑,耐得住寂寞

            Feedback

            # re: ACE vs Boost: Singleton的實(shí)現(xiàn)  回復(fù)  更多評(píng)論   

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


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


            C++中:
            singleton這種模式能"直接"解決的問(wèn)題只有1;利用C++語(yǔ)言機(jī)制 —— private —— 來(lái)限制實(shí)例數(shù)量。
            而問(wèn)題1也是眾多文章討論得最多的, 簡(jiǎn)單嘛……

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



            boost這種實(shí)現(xiàn),只能解決1, 在一定條件下,能解決4。
            如果遇見(jiàn)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."
            如果這種條件能被滿足, 直接使用全局變量同樣是線程安全的。

            如果真的需要解決問(wèn)題1,以及難看的全局變量:
            可以將這個(gè)全局變量以及類(lèi)的定義放在單一翻譯單元中,并在該翻譯但與實(shí)現(xiàn)若干wrapper函數(shù)即可。

            同樣, 對(duì)于問(wèn)題2和3, 全局變量也通常會(huì)壞事。

            綜上, boost.singleton, 簡(jiǎn)直就是為模式而模式。

            # re: ACE vs Boost: Singleton的實(shí)現(xiàn)  回復(fù)  更多評(píng)論   

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

            # re: ACE vs Boost: Singleton的實(shí)現(xiàn)  回復(fù)  更多評(píng)論   

            2009-09-22 14:29 by OwnWaterloo
            @陳梓瀚(vczh)
            沒(méi)明白。 這個(gè)條件能保證什么? 編譯時(shí)的依賴(lài)關(guān)系?

            # re: ACE vs Boost: Singleton的實(shí)現(xiàn)  回復(fù)  更多評(píng)論   

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

            # re: ACE vs Boost: Singleton的實(shí)現(xiàn)  回復(fù)  更多評(píng)論   

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

            # re: ACE vs Boost: Singleton的實(shí)現(xiàn)  回復(fù)  更多評(píng)論   

            2009-09-24 17:45 by 凡客誠(chéng)品
            便于你的代碼以后復(fù)用,久而久之組織成類(lèi)庫(kù)

            # re: ACE vs Boost: Singleton的實(shí)現(xiàn)  回復(fù)  更多評(píng)論   

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

            # re: ACE vs Boost: Singleton的實(shí)現(xiàn)  回復(fù)  更多評(píng)論   

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

            # re: ACE vs Boost: Singleton的實(shí)現(xiàn)  回復(fù)  更多評(píng)論   

            2009-12-03 16:06 by 金慶
            以上DCL單件的實(shí)現(xiàn)可能是有缺陷。但我不是很確信。因?yàn)锳CE確實(shí)是這樣實(shí)現(xiàn)的。按我的理解,加個(gè)volatile就好了。“The "Double-Checked Locking is Broken" Declaration”( http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html )一文可作參考。不知adah所說(shuō)的批駁是否指這一點(diǎn)?
            久久国产成人精品麻豆| 久久99国产精品久久| 久久久久黑人强伦姧人妻| 亚洲中文字幕无码久久综合网| 99久久国产主播综合精品| 久久久无码精品亚洲日韩按摩 | 色婷婷噜噜久久国产精品12p| 久久99国内精品自在现线| 亚洲精品无码久久久久久| 久久精品国产亚洲AV忘忧草18| 性做久久久久久久久浪潮| 亚洲午夜福利精品久久| 欧美激情一区二区久久久| 久久久国产精华液| 91久久精品国产成人久久| 久久久久久久亚洲Av无码| 久久久久免费视频| 国产成人精品久久综合| 久久国产精品偷99| 999久久久免费精品国产| 99国内精品久久久久久久| 久久久一本精品99久久精品66| 一本一道久久a久久精品综合| 99国产欧美久久久精品蜜芽| 亚洲精品乱码久久久久久自慰| 一级做a爰片久久毛片免费陪| 国产精品免费久久久久影院| 久久不射电影网| 99精品久久精品| 久久天天躁狠狠躁夜夜96流白浆 | 青青青伊人色综合久久| 国产精品欧美久久久久天天影视| 日产精品99久久久久久| 国产成人精品久久| 一本色道久久综合亚洲精品| 久久久久久久免费视频| 色播久久人人爽人人爽人人片AV| 亚洲国产精品无码久久青草| 亚洲欧美一级久久精品| 7777精品伊人久久久大香线蕉| 色偷偷91久久综合噜噜噜噜|