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

            Where there is a dream ,there is hope

              C++博客 :: 首頁 :: 聯系 :: 聚合  :: 管理
              64 Posts :: 0 Stories :: 8 Comments :: 0 Trackbacks

            常用鏈接

            留言簿(1)

            我參與的團隊

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            溫故而知新,總結了多種單例模式的寫法,考慮上多線程沒有非常完美的,各取所需吧

            //1.
            //Easy ,<<Pattern Design>>
            //Q:
            //When to free the m_singleton space?
            //when to execute the destruct function?
            //A:We can call getSingleton() at the end of the progress, 
            //then delete the pointer and free the space, 
            //but ,Here is a problem:Someone will call getSingleton() after 
            //we delete it。How to avoid these?
            class Singleton
            {
                
            public:
                    
            static Singleton* getSingleton()
                    
            {
                        
            if(NULL==m_singleton)
                            
            {
                                m_singleton
            =new Singleton();
                            }

                            
            return m_singleton;
                    }

                
            private:
                    Singleton()
            {}
                    
            ~Singleton(){}
                
            private:
                    
            static Singleton* m_singleton;        
            }
            ;
            //singleton.cpp
            singleton* Singleton::m_singleton=NULL;
            //Because ,at the end of the program, all globle vars will be destructed. ,
            //and all the static vars .we can use this point, define a static var in
            //the Singleton class ,it is used to delete the Singleton class .

            //2.-----------
            class Singleton
            {
                
            public:
                    staitc Singleton
            * getSingleton();
                
            private:
                    Singleton();
                    
            ~Singleton();
                    
            static Singleton* m_singleton;
                    
            class Garbo//Just used for delete the static var
                    {
                        
            public:
                            Garbo();
                            
            ~Garbo()
                            
            {
                                
            if(Singleton::m_singleton!=NULL)
                                    
            {
                                        delete Singleton::m_singleton;
                                        Singleton::m_singleton
            =NULL;
                                  }

                            }

                    }

                    staitc Garbo m_garbo;
            //define a static var,
            }

            //singleton.cpp
            Garbo Singleton::m_garbo;
            //

            //3.---------------
            class Singleton
            {
                
            public:
                staitc Singleton
            * getSingletonPtr()
                
            {
                    
            if(NULL==m_singleton)
                        
            {
                            m_singleton.reset(
            new Singleton);
                        }

                        
            return m_singleton.get();
                }

                
            protected:
                    Singleton()
            {}
                    
            virtual ~Singleton(){}
                    
                    friend 
            class auto_ptr<Singleton>;
                    
            static auto_ptr<Singleton> m_singleton;
            }
            ;

            //singleton.cpp
            auto_ptr<Singleton> Singleton::m_singleton=NULL;
                


            //4.
            class Singleton
            {
                
            public:
                    
            static Singleton* getSingletonPtr()
                    
            {
                        staitc Singleton singleton;
                        
            return &singleton;
                    }

                
            private:
                    Singleton()
            {}
                    
            ~Singleton(){}
            }


            //5.
            class Singleton
            {
                
            public:
                    staitc Singleton
            & getSingleton
                    
            {
                        
            return m_singleton;
                    }

                
            private:
                    staitc Singleton m_singleton;
                    Singleton()
            {}
                    
            }



            //6.---------thread safe

            #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()
            {
                   
            if0 == _instance.get() )
                   
            {
                          CResGuard::CGuard gd(_rs);
                          
            if0== _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 類主要的功能是線程訪問同步,代碼如下:
            /******************************************************************************

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

             
             
            #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
            }
            ;

            //7. most used
            template <typename T>
            class Singleton
            {
            public :
                
            static T& getSingleton()
                
            {
                    
            return *m_singlon;
                }


                
            static T* getSingletonPtr()
                
            {
                    
            return m_singlon;
                }

            protected:

                Singleton()
                
            {
                    m_singlon
            =static_cast<T*>(this);
                }


                
            ~Singleton()
                
            {
                    m_singlon
            =NULL;
                }

            private:
                Singleton(
            const Singleton&){}

                Singleton
            & operator=(const Singleton&){}

                
            static T* m_singlon;
            }
            ;

            template
            <typename T>
            T
            * Singleton<T>::m_singlon = NULL;
            posted on 2011-03-09 16:18 IT菜鳥 閱讀(328) 評論(0)  編輯 收藏 引用 所屬分類: C/C++
            一本久久a久久精品综合夜夜 | 久久久亚洲欧洲日产国码aⅴ| 麻豆久久| 久久香综合精品久久伊人| 狠狠狠色丁香婷婷综合久久俺| 日本一区精品久久久久影院| 欧美与黑人午夜性猛交久久久| 久久久久久久波多野结衣高潮| 99久久国语露脸精品国产| 久久精品女人天堂AV麻| 东京热TOKYO综合久久精品| 久久国产精品视频| 国产成人久久AV免费| 热综合一本伊人久久精品| 97久久国产亚洲精品超碰热| 久久亚洲国产成人影院网站| 国产精品久久影院| 久久久久久久久久久| 久久精品国产99国产精品 | 国产亚洲精午夜久久久久久| 久久久亚洲欧洲日产国码是AV| 国产精品免费久久久久久久久 | 久久青青草原综合伊人| 国色天香久久久久久久小说| 久久久久国产视频电影| 99久久免费国产精品热| 亚洲精品午夜国产VA久久成人| 久久久久一本毛久久久| 99久久精品国产一区二区三区 | 亚洲av伊人久久综合密臀性色| 久久久久99精品成人片| 久久国产成人午夜AV影院| 久久99精品国产麻豆蜜芽| 精品久久香蕉国产线看观看亚洲| 人妻精品久久久久中文字幕69 | 久久午夜无码鲁丝片| 国产激情久久久久久熟女老人| 精品久久久久久无码不卡| 亚洲国产香蕉人人爽成AV片久久| 精品无码久久久久久久久久| 国产精品丝袜久久久久久不卡|