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

            游戲人生

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

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

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

            本文同步自游戲人生

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

            o ACE的實現(xiàn)

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

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

            如果你能夠準確的區(qū)分以下三種實現(xiàn)的弊端和隱患,對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實現(xiàn)。

            o Boost的實現(xiàn)

            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;

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

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

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

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

            Feedback

            # re: ACE vs Boost: Singleton的實現(xiàn)  回復  更多評論   

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


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


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

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



            boost這種實現(xiàn),只能解決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,以及難看的全局變量:
            可以將這個全局變量以及類的定義放在單一翻譯單元中,并在該翻譯但與實現(xiàn)若干wrapper函數(shù)即可。

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

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

            # re: ACE vs Boost: Singleton的實現(xiàn)  回復  更多評論   

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

            # re: ACE vs Boost: Singleton的實現(xiàn)  回復  更多評論   

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

            # re: ACE vs Boost: Singleton的實現(xiàn)  回復  更多評論   

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

            # re: ACE vs Boost: Singleton的實現(xiàn)  回復  更多評論   

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

            # re: ACE vs Boost: Singleton的實現(xiàn)  回復  更多評論   

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

            # re: ACE vs Boost: Singleton的實現(xiàn)  回復  更多評論   

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

            # re: ACE vs Boost: Singleton的實現(xiàn)  回復  更多評論   

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

            # re: ACE vs Boost: Singleton的實現(xiàn)  回復  更多評論   

            2009-12-03 16:06 by 金慶
            以上DCL單件的實現(xiàn)可能是有缺陷。但我不是很確信。因為ACE確實是這樣實現(xiàn)的。按我的理解,加個volatile就好了。“The "Double-Checked Locking is Broken" Declaration”( http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html )一文可作參考。不知adah所說的批駁是否指這一點?
            久久99国产精品久久99果冻传媒| 国产成人精品久久亚洲高清不卡 国产成人精品久久亚洲高清不卡 国产成人精品久久亚洲 | 国产99久久精品一区二区| 99久久精品午夜一区二区| 国产精品久久久天天影视香蕉| 久久精品视频一| 蜜桃麻豆www久久| 无码伊人66久久大杳蕉网站谷歌| 一本久久久久久久| 久久人人爽人人爽人人AV东京热| 国产精品综合久久第一页| 久久棈精品久久久久久噜噜| 久久久久亚洲爆乳少妇无| 国产精品久久久久久久久鸭| 久久久久久久精品成人热色戒| 26uuu久久五月天| 久久久久久亚洲精品成人| 欧美久久亚洲精品| 国产99久久久国产精免费| 国产Av激情久久无码天堂| 午夜天堂精品久久久久| 午夜精品久久久久久影视riav| 久久久久久国产a免费观看不卡| 精品无码久久久久国产| 久久综合九色综合网站| 99久久国产综合精品女同图片| 久久综合视频网站| 久久婷婷五月综合色99啪ak| 国产91久久综合| 久久国产免费直播| 国产日韩久久久精品影院首页| 伊人色综合久久| 99久久婷婷国产综合精品草原 | 久久香蕉国产线看观看99| 久久久久99精品成人片试看| 亚洲精品国产字幕久久不卡| 思思久久精品在热线热| 精品国产乱码久久久久久呢 | 中文精品久久久久国产网址| 久久久久久a亚洲欧洲aⅴ| 久久91精品国产91久久小草|