• <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>
            posts - 45,  comments - 232,  trackbacks - 0

            Singleton模式是常用的設(shè)計(jì)模式之一,但是要實(shí)現(xiàn)一個(gè)真正實(shí)用的設(shè)計(jì)模式卻也不是件容易的事情。

            1.         標(biāo)準(zhǔn)的實(shí)現(xiàn)

            class Singleton

            {

            public:

                   static Singleton * Instance()

                   {

                          if( 0== _instance)

                          {

                                 _instance = new Singleton;

                          }

                          return _instance;

                   }

            protected:

                   Singleton(void)

                   {

                   }

                   virtual ~Singleton(void)

                   {

                   }

                   static Singleton* _instance;

            };

                   這是教科書(shū)上使用的方法??雌饋?lái)沒(méi)有什么問(wèn)題,其實(shí)包含很多的問(wèn)題。下面我們一個(gè)一個(gè)的解決。

            2.         自動(dòng)垃圾回收

            上面的程序必須記住在程序結(jié)束的時(shí)候,釋放內(nèi)存。為了讓它自動(dòng)的釋放內(nèi)存,我們引入auto_ptr改變它。

            #include <memory>

            #include <iostream>

            using namespace std;

            class Singleton

            {

            public:

                   static Singleton * Instance()

                   {

                          if( 0== _instance.get())

                          {

                                 _instance.reset( new Singleton);

                          }

                          return _instance.get();

                   }

            protected:

                   Singleton(void)

                   {

                          cout <<"Create Singleton"<<endl;

                   }

                   virtual ~Singleton(void)

                   {

                          cout << "Destroy Singleton"<<endl;

                   }

                   friend class auto_ptr<Singleton>;

                   static auto_ptr<Singleton> _instance;

            };

            //Singleton.cpp

            auto_ptr<Singleton> Singleton::_instance;

            3.         增加模板

            在我的一個(gè)工程中,有多個(gè)的Singleton類,對(duì)Singleton類,我都要實(shí)現(xiàn)上面這一切,這讓我覺(jué)得煩死了。于是我想到了模板來(lái)完成這些重復(fù)的工作。

            現(xiàn)在我們要添加本文中最吸引人單件實(shí)現(xiàn):

            /********************************************************************

                (c) 2003-2005 C2217 Studio

                Module:    Singleton.h

                Author:     Yangjun D.

                Created:    9/3/2005   23:17

                Purpose:    Implement singleton pattern

                History:

            *********************************************************************/

            #pragma once

             

            #include <memory>

            using namespace std;

            using namespace C2217::Win32;

             

            namespace C2217

            {

            namespace Pattern

            {

            template <class T>

            class Singleton

            {

            public:

                   static inline T* instance();

                  

            private:

                   Singleton(void){}

                   ~Singleton(void){}

                   Singleton(const Singleton&){}

                   Singleton & operator= (const Singleton &){}

             

                   static auto_ptr<T> _instance;

            };

             

            template <class T>

            auto_ptr<T> Singleton<T>::_instance;

             

            template <class T>

             inline T* Singleton<T>::instance()

            {

                   if( 0== _instance.get())

                   {

                          _instance.reset ( new T);

                   }

                  

                   return _instance.get();

            }

             

            //Class that will implement the singleton mode,

            //must use the macro in it's delare file

            #define DECLARE_SINGLETON_CLASS( type ) \

                   friend class auto_ptr< type >;\

                   friend class Singleton< type >;

            }

            }

             

            4.         線程安全

            上面的程序可以適應(yīng)單線程的程序。但是如果把它用到多線程的程序就會(huì)發(fā)生問(wèn)題。主要的問(wèn)題在于同時(shí)執(zhí)行_instance.reset ( new T); 就會(huì)同時(shí)產(chǎn)生兩個(gè)新的對(duì)象,然后馬上釋放一個(gè),這跟Singleton模式的本意不符。所以,你需要更加安全的版本:

            /********************************************************************

                (c) 2003-2005 C2217 Studio

                Module:    Singleton.h

                Author:     Yangjun D.

                Created:    9/3/2005   23:17

                Purpose:    Implement singleton pattern

                History:

            *********************************************************************/

            #pragma once

             

            #include <memory>

            using namespace std;

            #include "Interlocked.h"

            using namespace C2217::Win32;

             

            namespace C2217

            {

            namespace Pattern

            {

            template <class T>

            class Singleton

            {

            public:

                   static inline T* instance();

                  

            private:

                   Singleton(void){}

                   ~Singleton(void){}

                   Singleton(const Singleton&){}

                   Singleton & operator= (const Singleton &){}

             

                   static auto_ptr<T> _instance;

                   static CResGuard _rs;

            };

             

            template <class T>

            auto_ptr<T> Singleton<T>::_instance;

             

            template <class T>

            CResGuard Singleton<T>::_rs;

             

            template <class T>

             inline T* Singleton<T>::instance()

            {

                   if( 0 == _instance.get() )

                   {

                          CResGuard::CGuard gd(_rs);

                          if( 0== _instance.get())

                          {

                                 _instance.reset ( new T);

                          }

                   }

                   return _instance.get();

            }

             

            //Class that will implement the singleton mode,

            //must use the macro in it's delare file

            #define DECLARE_SINGLETON_CLASS( type ) \

                   friend class auto_ptr< type >;\

                   friend class Singleton< type >;

            }

            }

                   CresGuard 類主要的功能是線程訪問(wèn)同步,代碼如下:

            /******************************************************************************

            Module:  Interlocked.h

            Notices: Copyright (c) 2000 Jeffrey Richter

            ******************************************************************************/

             

             

            #pragma once

            ///////////////////////////////////////////////////////////////////////////////

             

            // Instances of this class will be accessed by multiple threads. So,

            // all members of this class (except the constructor and destructor)

            // must be thread-safe.

            class CResGuard {

            public:

               CResGuard()  { m_lGrdCnt = 0; InitializeCriticalSection(&m_cs); }

               ~CResGuard() { DeleteCriticalSection(&m_cs); }

             

               // IsGuarded is used for debugging

               BOOL IsGuarded() const { return(m_lGrdCnt > 0); }

             

            public:

               class CGuard {

               public:

                  CGuard(CResGuard& rg) : m_rg(rg) { m_rg.Guard(); };

                  ~CGuard() { m_rg.Unguard(); }

             

               private:

                  CResGuard& m_rg;

               };

             

            private:

               void Guard()   { EnterCriticalSection(&m_cs); m_lGrdCnt++; }

               void Unguard() { m_lGrdCnt--; LeaveCriticalSection(&m_cs); }

             

               // Guard/Unguard can only be accessed by the nested CGuard class.

               friend class CResGuard::CGuard;

             

            private:

               CRITICAL_SECTION m_cs;

               long m_lGrdCnt;   // # of EnterCriticalSection calls

            };

             

             

            ///////////////////////////////////////////////////////////////////////////////

            5.         實(shí)用方法

            比如你有一個(gè)需要實(shí)現(xiàn)單件模式的類,就應(yīng)該這樣實(shí)現(xiàn):

            #pragma once

            #include "singleton.h"

            using namespace C2217::Pattern;

             

            class ServiceManger

            {

            public:

                   void Run()

                   {

                   }

            private:

                   ServiceManger(void)

                   {

                   }

                   virtual ~ServiceManger(void)

                   {

                   }

                   DECLARE_SINGLETON_CLASS(ServiceManger);

            };

             

            typedef Singleton<ServiceManger> SSManger;

             

            在使用的時(shí)候很簡(jiǎn)單,跟一般的Singleton實(shí)現(xiàn)的方法沒(méi)有什么不同。

            int _tmain(int argc, _TCHAR* argv[])

            {

                    SSManger::instance()->Run();

            }

             

            一個(gè)簡(jiǎn)單的Singleton模式的實(shí)現(xiàn),可以看到C++語(yǔ)言背后隱藏的豐富的語(yǔ)意,我希望有人能實(shí)現(xiàn)一個(gè)更好的Singleton讓大家學(xué)習(xí)。我從一開(kāi)始實(shí)現(xiàn)Singleton類的過(guò)程,其實(shí)就是我學(xué)習(xí)C++的過(guò)程,越是深入越覺(jué)得C++了不起。

             

             

            posted on 2005-09-20 22:14 天下無(wú)雙 閱讀(55501) 評(píng)論(36)  編輯 收藏 引用 所屬分類: C/C++

            FeedBack:
            # re: C++完美實(shí)現(xiàn)Singleton模式
            2005-09-21 00:31 | mexo
            似乎可以這樣:
            template <class T>
            class Singleton
            {
            public:
            static inline T& instance()
            {
            static T _instance;
            return _instance;
            }

            private:
            Singleton(void);
            ~Singleton(void);
            Singleton(const Singleton<T>&);
            Singleton<T>& operator= (const Singleton<T> &);
            };
              回復(fù)  更多評(píng)論
              
            # re: C++完美實(shí)現(xiàn)Singleton模式
            2005-09-21 08:52 | 天下無(wú)雙
            Cool!
            比我的實(shí)現(xiàn)簡(jiǎn)單很多,并且也達(dá)到了同樣的效果,不佩服都不行.  回復(fù)  更多評(píng)論
              
            # re: C++完美實(shí)現(xiàn)Singleton模式
            2005-12-22 18:08 | 小明
            mexo 說(shuō)的實(shí)現(xiàn)方式Meyer(Effective c++)提出來(lái)的

            這種方式并不是沒(méi)有多線程問(wèn)題的  回復(fù)  更多評(píng)論
              
            # 還不夠完美
            2006-01-09 21:51 | NoSound
            mexo提出的方案的確不錯(cuò),但是好象也并不完美(我不是指多線程解決方案),因?yàn)樗涯0孱惖臉?gòu)造函數(shù)放在私有段里了,如果放在protected段里就好得多,因?yàn)槟愕念惪梢詮哪0孱惱^承,這樣就不再需要你的那個(gè) typedef Singleton<ServiceManger> SSManger;定義了。示例如下:
            template <class T>
            class Singleton {
            public:
            static T& instance() {
            static T _instance;
            return _instance;
            }
            protected:
            Singleton(void) {}
            virtual ~Singleton(void) {}
            Singleton(const Singleton<T>&); //不實(shí)現(xiàn)
            Singleton<T>& operator= (const Singleton<T> &); //不實(shí)現(xiàn)
            };
            --------------------------------
            下面是一個(gè)需要做為單例的類,只需從Singleton繼承即可
            class Test : public Singleton<Test> {
            public:
            void foo();
            private:
            Test();
            ~Test();
            friend class Singleton<Test>;
            };
            ----------------------
            這樣,別人在使用的時(shí)候,只需要寫(xiě)
            Test::instance().foo();
            而再也不需要寫(xiě):
            Singleton<Test>::instance().foo();
            或者
            typedef Singleton<Test> STest;
            STest::instance().foo();
            -----------------------------
              回復(fù)  更多評(píng)論
              
            # re: C++完美實(shí)現(xiàn)Singleton模式
            2006-01-17 16:22 | 3×7=51
            我猜樓主一定沒(méi)看過(guò)Andrei Alexandrescu寫(xiě)的Modern C++ Design  回復(fù)  更多評(píng)論
              
            # re: C++完美實(shí)現(xiàn)Singleton模式
            2006-01-18 09:02 | 天下無(wú)雙
            我是寫(xiě)些Singleton的實(shí)現(xiàn), 后來(lái)有機(jī)會(huì)看來(lái)你說(shuō)的那本書(shū), 感覺(jué)思想差不多,不謀而和。  回復(fù)  更多評(píng)論
              
            # re: C++完美實(shí)現(xiàn)Singleton模式
            2006-01-27 11:07 | cyt
            Singleton在多線程方面,還漏了一篇文章沒(méi)有看:
            《Double-Checked Locking,Threads,Compiler Optimizations,and More》(Scott Meyers)

            主要意思就是在:_instance.reset ( new T);
            在編譯器優(yōu)化情況下,_instance先設(shè)了指針內(nèi)容,然后再進(jìn)行構(gòu)造函數(shù)。如果有第二個(gè)線程這時(shí)候進(jìn)行訪問(wèn),_instance內(nèi)容為非空,于是跳過(guò)了第一個(gè)if( 0 == _instance.get() )。但實(shí)際上對(duì)象還是沒(méi)有構(gòu)造完整。

            實(shí)際上我們多是先用
            std::auto_ptr<T> _au(new T);
            _instance = _au;
              回復(fù)  更多評(píng)論
              
            # re: C++完美實(shí)現(xiàn)Singleton模式
            2006-03-23 17:12 | cat5
            如果已經(jīng)作了多線程保護(hù),_instance.reset ( new T); 和std::auto_ptr<T> _au(new T);
            _instance = _au;
            不一樣嗎?  回復(fù)  更多評(píng)論
              
            # re: C++完美實(shí)現(xiàn)Singleton模式
            2006-03-23 18:33 | cat5
            Sorry,
            if( 0 == _instance.get() )
            {
            CResGuard::CGuard gd(_rs);
            if( 0== _instance.get())
            {
            _instance.reset ( new T);
            }
            如果把CResGuard::CGuard gd(_rs);放到第一句即可解決,但卻帶來(lái)了效率問(wèn)題。這就是DCL要解決的內(nèi)容。  回復(fù)  更多評(píng)論
              
            # re: C++完美實(shí)現(xiàn)Singleton模式
            2006-12-18 16:45 | DENGYangjun
            繼承的方法還是比較好用的,現(xiàn)在又體會(huì)了。但有時(shí)候繼承顯得比較亂,好像那個(gè)情形都有自己適用的情形。  回復(fù)  更多評(píng)論
              
            # re: C++完美實(shí)現(xiàn)Singleton模式
            2007-01-29 14:32 | 李錦俊
            用智能指針,意味著不能自己控制何時(shí)釋放,如果幾個(gè)不同的Singleton需要按順序創(chuàng)建并且釋放,這種實(shí)現(xiàn)方式就不太好了  回復(fù)  更多評(píng)論
              
            # re: C++完美實(shí)現(xiàn)Singleton模式
            2007-01-29 18:36 | DENGYangjun
            如果你把指針置空,就會(huì)釋放對(duì)象,這個(gè)應(yīng)該不是問(wèn)題??梢约右粋€(gè)這樣的接口。  回復(fù)  更多評(píng)論
              
            # re: C++完美實(shí)現(xiàn)Singleton模式
            2007-02-06 15:19 | sz
            本著簡(jiǎn)單高效的原則, boost::singleton是singleton模式的又一種實(shí)現(xiàn), 它基于以下假設(shè), 一個(gè)良好的設(shè)計(jì), 在進(jìn)入main函數(shù)前應(yīng)該是單線程的,此時(shí),我們可以采用和全局變量相似的辦法來(lái)使用singleton,因?yàn)樗械娜肿兞吭谶M(jìn)入main以前已經(jīng)全部初始化。這樣我們就避開(kāi)了多線程的競(jìng)爭(zhēng)條件. 但直接使用全局變量有一個(gè)嚴(yán)重的缺陷,就是當(dāng)你使用全局變量時(shí),你并不能保證它已經(jīng)得到初始化,這種情況發(fā)生在 static code中。(static code是boost文檔中的用語(yǔ), 我想它是指在進(jìn)入main函數(shù)以前要執(zhí)行的代碼)。

            boost::singlton實(shí)現(xiàn)的關(guān)鍵有兩點(diǎn)
            (1) sington 在進(jìn)入main函數(shù)前初始化.
            (2)第一次使用時(shí), singlton已得到正確的初始化(包括在static code中情況).

             boost中的實(shí)現(xiàn)代碼如下所示:
             
            template <typename T>
            struct singleton
            {
            private:
            struct object_creator
            {
            object_creator() { singleton<T>::instance(); }
            inline void do_nothing() const { }
            };
            static object_creator create_object;
            singleton();
            public:
            typedef T object_type;
            static object_type & instance()
            {
            static object_type obj;
            create_object.do_nothing();
            return obj;
            }
            };

            template <typename T> typename singleton<T>::object_creator singleton<T>::create_object;

            我沒(méi)看明白,誰(shuí)能給分析分析呀!  回復(fù)  更多評(píng)論
              
            # re: C++完美實(shí)現(xiàn)Singleton模式
            2007-02-08 11:25 | 天下無(wú)雙
            我想他的意思就是在主函數(shù)運(yùn)行以前,初始化單件類,確實(shí)能夠克服多線程的問(wèn)題。不過(guò)呢,這就失去了,惰性初始化的優(yōu)點(diǎn)-即需要的用的時(shí)候才初始化。  回復(fù)  更多評(píng)論
              
            # re: C++完美實(shí)現(xiàn)Singleton模式
            2007-05-23 00:31 | ??
            @_@程序用了大半天才開(kāi)始用singleton的情況多么?  回復(fù)  更多評(píng)論
              
            # re: C++完美實(shí)現(xiàn)Singleton模式
            2007-05-23 01:03 | eXile
            這種方式遠(yuǎn)談不上完美,一般C++的singleton實(shí)現(xiàn)的最大缺陷就是釋放的順序依賴問(wèn)題,而象ModerC++Design那樣,又太復(fù)雜了,不實(shí)用。
            所以,我現(xiàn)在都是用main()中的局部變量來(lái)模擬全局變量,能很好用于單件模式 。  回復(fù)  更多評(píng)論
              
            # re: C++完美實(shí)現(xiàn)Singleton模式
            2007-05-23 01:07 | eXile
            而且前一向老外有一篇文章,說(shuō)了一下什么情況下,雙重鎖會(huì)失敗,我沒(méi)有細(xì)看,總之也是不安全的  回復(fù)  更多評(píng)論
              
            # re: C++完美實(shí)現(xiàn)Singleton模式
            2007-06-05 15:23 | 天下無(wú)雙
            請(qǐng)eXile講講具體怎么用局部變量模擬全局變量。謝謝  回復(fù)  更多評(píng)論
              
            # re: C++完美實(shí)現(xiàn)Singleton模式
            2007-08-25 18:09 | 螞蟻終結(jié)者
            大概看了一下,除了cyt說(shuō)的Double-Checked Locking,編譯器可能會(huì)混亂代碼的執(zhí)行次序,即先設(shè)置_instance指針的內(nèi)容再執(zhí)行構(gòu)造函數(shù)。
            還至少有兩個(gè)問(wèn)題:
            1.
            auto_ptr在某些情況下會(huì)出問(wèn)題,假設(shè)有某個(gè)單例類A在析構(gòu)時(shí)調(diào)用另外一個(gè)單例類Log來(lái)記錄一些日志信息,因?yàn)樵诔绦蚪Y(jié)束時(shí)靜態(tài)成員的析構(gòu)可能會(huì)是任意次序,單例類Log很有可能在A調(diào)用析構(gòu)函數(shù)之前就析構(gòu)了,后果就不用說(shuō)吧。

            當(dāng)然解決方法很簡(jiǎn)單,用C標(biāo)準(zhǔn)庫(kù)的atexit就行了,atexit函數(shù)原型如下:
            int atexit(void (*func )());
            用atexit可以注冊(cè)任意多個(gè)函數(shù),當(dāng)程序結(jié)束時(shí)會(huì)按LIFO的次序調(diào)用注冊(cè)的函數(shù)。這樣就能保證多個(gè)有依賴關(guān)系的單例類的析構(gòu)順序。
            我們修改Singleton的實(shí)現(xiàn),加上:
            static void Destroy() {
            if ( _instance != 0 ) {
            delete _instance;
            _instance = 0;
            }
            }
            將Instance實(shí)現(xiàn)修改為類似代碼:
            static T& Instance() {
            if (0 == _instance) {
            Lock lock(_cs);
            if (0 == _instance) {
            _instance = new T();
            atexit(Destroy);
            }
            }
            return *_instance;
            }

            2.

            _instance.reset ( new T);
            或者
            _instance = new T();
            這里其實(shí)還會(huì)有問(wèn)題,在C++中對(duì)指針賦值操作并不能保證是原子操作,如果有某個(gè)線程1執(zhí)行到這里,賦值到一半,線程1掛起,線程2開(kāi)始執(zhí)行,這時(shí)候
            _instance可能處于任何狀態(tài),0 == _instance 也許為true,線程2于是return *_instance,這時(shí)候就會(huì)有問(wèn)題了...

            設(shè)計(jì)一個(gè)完美的Singleton也許比想象的要難的多  回復(fù)  更多評(píng)論
              
            # re: C++完美實(shí)現(xiàn)Singleton模式
            2007-08-25 18:22 | 螞蟻終結(jié)者
            貌似還有一點(diǎn)忘了,就是防治編譯器多線程環(huán)境下的優(yōu)化,
            這正是volatile關(guān)鍵詞的用處

            static auto_ptr<T> _instance;
            或者用atexit后改成
            static T * _instance;
            都可能會(huì)有問(wèn)題,因?yàn)槎嗑€程環(huán)境下的變量容易被緩存
            所以最好加上volatile

            static volatile auto_ptr<T> _instance;
            或者用atexit后改成
            static T * volatile _instance;  回復(fù)  更多評(píng)論
              
            # re: C++完美實(shí)現(xiàn)Singleton模式
            2007-10-24 09:14 | 老五
            # re: C++完美實(shí)現(xiàn)Singleton模式
            2007-10-30 16:28 | chjw_ch
            這種方法是不行的在某些情況下是不能夠使用的,例如
            class SingletonClass : public Singleton <SingletonClass>
            {
            ....
            };

            @mexo
              回復(fù)  更多評(píng)論
              
            # re: C++完美實(shí)現(xiàn)Singleton模式
            2007-10-30 16:30 | chjw_ch
            這種方法是不行的在某些情況下是不能夠使用的,例如
            class SingletonClass : public Singleton <SingletonClass>
            {
            ....
            };
            針對(duì)的是
            # re: C++完美實(shí)現(xiàn)Singleton模式 2005-09-21 00:31 | mexo
            提出的方案  回復(fù)  更多評(píng)論
              
            # re: C++完美實(shí)現(xiàn)Singleton模式
            2008-03-25 20:33 | kendan
            學(xué)習(xí)了。。。。。  回復(fù)  更多評(píng)論
              
            # re: C++完美實(shí)現(xiàn)Singleton模式
            2009-03-09 09:43 | madmanahong
            別老研究Singleton了,沒(méi)啥意義,你的代碼中越多運(yùn)用了Singleton,越表示你的設(shè)計(jì)有問(wèn)題!

            只要為什么?去看《重構(gòu)與模式》這本書(shū)!  回復(fù)  更多評(píng)論
              
            # re: C++完美實(shí)現(xiàn)Singleton模式
            2009-04-17 10:45 | 創(chuàng)意產(chǎn)品網(wǎng)
            不錯(cuò)。  回復(fù)  更多評(píng)論
              
            # re: C++完美實(shí)現(xiàn)Singleton模式
            2010-01-18 16:19 | lxchust
            明顯的錯(cuò)誤:析構(gòu)函數(shù)被限定為protected怎么調(diào)用?如何析構(gòu)  回復(fù)  更多評(píng)論
              
            # re: C++完美實(shí)現(xiàn)Singleton模式
            2010-05-18 09:14 | QQ:124294272
            @NoSound
            程序調(diào)試通不啊,小兄弟  回復(fù)  更多評(píng)論
              
            # re: C++完美實(shí)現(xiàn)Singleton模式
            2010-07-27 17:00 | foratiw
            @NoSound
            恩,這個(gè)方法很簡(jiǎn)潔  回復(fù)  更多評(píng)論
              
            # re: C++完美實(shí)現(xiàn)Singleton模式
            2010-12-10 12:03 | 艾克斯の編碼者
            我是這樣的
            #pragma once
            #ifndef SINGLETON_H
            #define SINGLETON_H

            template<class T>
            class Singleton
            {
            public:
            static T& Instance();

            protected:
            Singleton(){}
            virtual ~Singleton(){}

            /**
            * 防止拷貝復(fù)制
            */
            private:
            Singleton(const Singleton &);
            Singleton & operator = (const Singleton &);
            };

            template
            T& Singleton::Instance()
            {
            /** 建立一個(gè)靜態(tài)對(duì)象 */
            static T instance;
            return instance;
            }

            #endif  回復(fù)  更多評(píng)論
              
            # re: C++完美實(shí)現(xiàn)Singleton模式
            2011-08-08 18:43 | 愛(ài)因斯坦
            @螞蟻終結(jié)者
            指針賦值不會(huì)賦值到一半問(wèn)題。
            只是構(gòu)造可能晚于指針賦值  回復(fù)  更多評(píng)論
              
            # re: C++完美實(shí)現(xiàn)Singleton模式
            2011-09-26 23:06 | 過(guò)客
            貌似離完美還差很遠(yuǎn),double-check本身就不能解決多線程問(wèn)題,有關(guān)析構(gòu)或構(gòu)造失敗的問(wèn)題都沒(méi)有考慮進(jìn)去,而且auto_ptr本身就不完美  回復(fù)  更多評(píng)論
              
            # re: C++完美實(shí)現(xiàn)Singleton模式
            2012-01-21 15:14 | Setekhid
            你這第一個(gè)范例對(duì)么?怎么我的codeblock不通過(guò)??  回復(fù)  更多評(píng)論
              
            # re: C++完美實(shí)現(xiàn)Singleton模式
            2012-02-18 23:16 | seckcoder
            文章很精彩. 評(píng)論更精彩  回復(fù)  更多評(píng)論
              
            # re: C++完美實(shí)現(xiàn)Singleton模式
            2012-09-08 09:06 | Wilson
            @mexo
            如果Singleton僅僅如此,那么很多的先鋒們真的早就該死了。可悲的不是如此。
            請(qǐng)問(wèn)此方法如何實(shí)現(xiàn)延后實(shí)例化?
              回復(fù)  更多評(píng)論
              
            # re: C++完美實(shí)現(xiàn)Singleton模式
            2014-11-18 16:45 | 過(guò)客
            isGuarded似乎忘記調(diào)用了。在new一個(gè)新的instance前應(yīng)該注意該線程是否可用。  回復(fù)  更多評(píng)論
              

            常用鏈接

            留言簿(15)

            隨筆分類

            隨筆檔案

            相冊(cè)

            我的其它領(lǐng)域Blog

            搜索

            •  

            積分與排名

            • 積分 - 205756
            • 排名 - 130

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            久久国产色av免费看| 国产精品99久久久久久猫咪| 四虎亚洲国产成人久久精品| 精品无码人妻久久久久久| 色天使久久综合网天天| 亚洲精品午夜国产VA久久成人| 日韩AV无码久久一区二区| 狠狠色婷婷综合天天久久丁香| 欧美伊香蕉久久综合类网站| 久久精品亚洲乱码伦伦中文| 免费精品久久天干天干| 青青青青久久精品国产 | 99久久国产亚洲高清观看2024| 国产亚洲成人久久| 久久精品国产亚洲AV香蕉| 久久夜色精品国产亚洲| 久久午夜夜伦鲁鲁片免费无码影视 | 国产成人精品久久| 日韩一区二区久久久久久| 18岁日韩内射颜射午夜久久成人| 美女写真久久影院| 99久久精品午夜一区二区| 久久人人添人人爽添人人片牛牛| 国产精品久久免费| 99国产欧美久久久精品蜜芽 | 久久久久久国产a免费观看黄色大片| 久久99精品久久久久久动态图| 国产精品成人99久久久久91gav | 久久九九精品99国产精品| 一本久久精品一区二区| 久久精品国产一区二区| 国产精品福利一区二区久久| 亚洲欧美日韩久久精品第一区| 久久久无码精品亚洲日韩软件| 久久综合丁香激情久久| 国产精品免费看久久久| 少妇精品久久久一区二区三区| 亚洲国产精品无码久久青草| 天天综合久久一二三区| 久久久久99精品成人片| 久久久精品国产Sm最大网站|