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

            那誰的技術博客

            感興趣領域:高性能服務器編程,存儲,算法,Linux內核
            隨筆 - 210, 文章 - 0, 評論 - 1183, 引用 - 0
            數據加載中……

            服務器公共庫開發--線程安全的singleton類, 可配置的線程鎖管理類

            在服務器開發中,大量的使用了singleton模式, 以我的工作為例, 用于打印log的類是一個singleton, 內存池管理器是一個singleton...雖然singleton模式實現起來不難, 但是為了避免重復開發, 我還是決定應該把這個類的實現單獨拿出來,同時, singleton類還需要支持多線程,但是我從來不寫多線程的服務器程序, 對多線程的支持可以通過預編譯宏來實現.我把操作多線程鎖, 以及singleton類都放在這篇文章中, 多線程鎖僅支持linux.

            threadmutex.h
            /********************************************************************
                created:    2008/08/01
                filename:     threadmutex.h
                author:        Lichuang
                            
                purpose:    線程鎖類, 由是否定義宏__USE_THREAD__來決定是否使用該類
            ********************************************************************
            */

            #ifndef __THREAD_MUTEX_H__
            #define __THREAD_MUTEX_H__

            #ifdef __USE_THREAD__
                #include 
            <pthread.h>
                
                
            #define THREAD_LOCK(tThreadMutex)     tThreadMutex.Lock()
                
            #define THREAD_UNLOCK(tThreadMutex)   tThreadMutex.UnLock()
            #else
                
            #define THREAD_LOCK(tThreadMutex)     
                
            #define THREAD_UNLOCK(tThreadMutex)   
            #endif

            class CThreadMutex
            {
            public:
                CThreadMutex();
                
            ~CThreadMutex();
                
            int Lock();
                
            int UnLock();

            private:
            #ifdef __USE_THREAD__
                pthread_mutex_t  m_tThreadMutex;
            #endif
            };

            #endif /* __THREAD_MUTEX_H__ */

            threadmutex.cpp
            /********************************************************************
                created:    2008/08/01
                filename:     threadmutex.h
                author:        Lichuang
                            
                purpose:    線程鎖類, 由是否定義宏__USE_THREAD__來決定是否使用該
                            線程鎖類
            ********************************************************************
            */

            #include 
            "threadmutex.h"

            #ifdef __USE_THREAD__

            CThreadMutex::CThreadMutex()
            {        
                ::pthread_mutex_init(
            &m_tThreadMutex, NULL);
            }

            CThreadMutex::
            ~CThreadMutex()
            {  
                ::pthread_mutex_destroy(
            &m_tThreadMutex);
            }

            int CThreadMutex::Lock()
            {
                
            return ::pthread_mutex_lock(&m_tThreadMutex);
            }

            int CThreadMutex::UnLock()
            {
                
            return ::pthread_mutex_unlock(&m_tThreadMutex);
            }

            #else

            CThreadMutex::CThreadMutex()
            {        
            }

            CThreadMutex::
            ~CThreadMutex()
            {  
            }

            int CThreadMutex::Lock()
            {
                
            return 0;
            }

            int CThreadMutex::UnLock()
            {
                
            return 0;
            }

            #endif

            singleton.h
            /********************************************************************
                created:    2008/08/01
                filename:     singleton.h
                author:        Lichuang
                            
                purpose:    實現單件模式的虛擬基類, 其它需要實現為singleton的類可以
                            繼承自這個類
                            支持多線程, 采用智能指針實現自動回收內存
            ********************************************************************
            */

            #ifndef __SINGLETON_H__
            #define __SINGLETON_H__

            #include 
            <memory>
            #include 
            "threadmutex.h"

            using namespace std;

            #define DECLARE_SINGLETON_CLASS( type ) \
                    friend 
            class auto_ptr< type >;  \
                    friend 
            class CSingleton< type >;

            template 
            <class T>
            class CSingleton
            {
            public:
                
            static T* GetInstance();

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

            protected:    
                friend 
            class auto_ptr<CSingleton>;

                
            static auto_ptr<T> m_pInstance;
                
            static CThreadMutex m_tThreadMutex;
            };

            template 
            <class T>
            auto_ptr
            <T> CSingleton<T>::m_pInstance;

            template 
            <class T>
            CThreadMutex CSingleton
            <T>::m_tThreadMutex;

            template 
            <class T>
            inline T
            * CSingleton<T>::GetInstance()
            {
                
            if (0 == m_pInstance.get())
                {
                    THREAD_LOCK(m_tThreadMutex);
                    
            if (0 == m_pInstance.get())
                    {
                        m_pInstance.reset(::
            new T);
                    }
                    THREAD_UNLOCK(m_tThreadMutex);
                }

                
            return m_pInstance.get();
            }

            #endif /* __SINGLETON_H__ */

            使用示例:

            #include 
            <iostream>
            #include 
            "singleton.h"

            using namespace std;

            class CTestSingleton
                : 
            public CSingleton<CTestSingleton>
            {
            public:

                
            void Set(int a)
                {
                    m_a 
            = a;
                }
                
            int Get()
                {
                    
            return m_a;
                }

            private:
                CTestSingleton()
                    : m_a(
            0)
                {

                }
                DECLARE_SINGLETON_CLASS(CTestSingleton)
            private:
                
            int m_a;
            };

            int main()
            {
                
            if (NULL == CTestSingleton::GetInstance())
                {
                    cout 
            << "GetInstance() error!" << endl;
                }

                cout 
            << "before set: " << CTestSingleton::GetInstance()->Get() << endl;

                CTestSingleton::GetInstance()
            ->Set(100);

                cout 
            << "after set: " << CTestSingleton::GetInstance()->Get() << endl;

                
            return 0;
            }


            posted on 2008-08-01 23:32 那誰 閱讀(5476) 評論(2)  編輯 收藏 引用 所屬分類: C\C++設計模式服務器設計

            評論

            # re: 服務器公共庫開發--線程安全的singleton類, 可配置的線程鎖管理類 [未登錄]  回復  更多評論   

            學習。。。
            2010-07-15 10:37 | 123

            # re: 服務器公共庫開發--線程安全的singleton類, 可配置的線程鎖管理類 [未登錄]  回復  更多評論   

            能請教下為什么用auto_ptr嗎?
            2011-03-16 20:19 | robin
            伊人久久综合成人网| 色欲综合久久躁天天躁蜜桃| 久久久久亚洲精品无码蜜桃 | 久久96国产精品久久久| 久久精品人人做人人爽电影蜜月 | 亚洲美日韩Av中文字幕无码久久久妻妇 | 88久久精品无码一区二区毛片 | 伊人久久综合热线大杳蕉下载| 久久丫精品国产亚洲av不卡| 久久精品国产亚洲AV影院| 久久婷婷五月综合色奶水99啪| 99久久综合国产精品免费| 久久精品视频一| 日本人妻丰满熟妇久久久久久| 人妻无码久久一区二区三区免费 | 99久久免费国产精品特黄| 日批日出水久久亚洲精品tv| 99久久夜色精品国产网站| 久久精品国产99国产精品澳门 | 久久美女人爽女人爽| 国产精品99久久久久久董美香| 色综合久久天天综线观看| 亚洲αv久久久噜噜噜噜噜| 国产999精品久久久久久| 久久久久久精品免费看SSS| 无码人妻久久一区二区三区免费| 精品久久久久久亚洲| 老男人久久青草av高清| 久久精品夜色噜噜亚洲A∨| 久久国产精品成人免费| 久久av无码专区亚洲av桃花岛| 久久精品夜色噜噜亚洲A∨| 久久成人影院精品777| 精品综合久久久久久888蜜芽| 久久精品国产亚洲AV久| 国产激情久久久久影院老熟女| 精品久久久噜噜噜久久久| 99久久国产精品免费一区二区| 狠狠综合久久综合88亚洲| 久久精品国产亚洲AV忘忧草18| 青青草原精品99久久精品66|