• <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++博客 :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
              64 Posts :: 0 Stories :: 8 Comments :: 0 Trackbacks

            常用鏈接

            留言簿(1)

            我參與的團(tuán)隊(duì)

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

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

            //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菜鳥 閱讀(324) 評論(0)  編輯 收藏 引用 所屬分類: C/C++
            亚洲国产精品无码久久一线| 亚洲国产成人精品久久久国产成人一区二区三区综 | 久久久久99精品成人片欧美 | 久久99国产精品久久99| 国产成人精品久久一区二区三区av| 99久久精品费精品国产| 亚洲精品无码久久毛片| 日韩久久久久久中文人妻 | 亚洲av伊人久久综合密臀性色| 精品国产乱码久久久久久郑州公司| 久久美女人爽女人爽| 亚洲人AV永久一区二区三区久久 | 亚洲国产精品成人久久蜜臀| 久久国产乱子伦免费精品| 久久国产精品波多野结衣AV| 伊人久久综合无码成人网| 国产精品久久午夜夜伦鲁鲁| 亚洲欧美久久久久9999| 色综合久久久久| 亚洲AV日韩精品久久久久久久| 久久精品免费网站网| 精品国产91久久久久久久| 久久精品国产2020| 日日狠狠久久偷偷色综合免费| 久久久久AV综合网成人| 无码人妻久久一区二区三区| 色婷婷噜噜久久国产精品12p| 四虎国产永久免费久久| 久久香蕉国产线看观看精品yw| 欧美与黑人午夜性猛交久久久| 国产亚洲精午夜久久久久久 | 99久久精品这里只有精品| 国产精品无码久久久久久| 一本色道久久88精品综合 | 久久亚洲欧美日本精品| 国内精品久久久久久99| 精品免费久久久久久久| 久久精品国产清高在天天线| 久久99精品国产自在现线小黄鸭| 亚洲午夜久久久久久噜噜噜| 亚洲精品乱码久久久久久蜜桃图片|