• <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
            大香网伊人久久综合网2020| 狠狠色丁香久久婷婷综合| 99精品久久精品| 91久久精品国产免费直播| 久久精品视频91| 久久午夜羞羞影院免费观看| 99久久精品国产综合一区 | 久久久国产精品| 伊人久久大香线蕉综合影院首页| 国产精品久久久久久影院| 天堂无码久久综合东京热| 久久午夜羞羞影院免费观看| 精品久久人人做人人爽综合| 无码人妻久久一区二区三区蜜桃| 久久青青草原综合伊人| 无码AV中文字幕久久专区| 激情久久久久久久久久| 国内精品九九久久久精品| 久久精品久久久久观看99水蜜桃| 国产精品99久久久久久猫咪| 久久综合狠狠综合久久综合88| 人妻系列无码专区久久五月天| 久久九九青青国产精品| 亚洲国产精品无码久久SM| 一本久久综合亚洲鲁鲁五月天亚洲欧美一区二区 | 久久亚洲精品无码aⅴ大香| 国产高清国内精品福利99久久| 久久99国产综合精品女同| 国色天香久久久久久久小说| 青青热久久国产久精品| 久久国产热这里只有精品| 久久99国产综合精品女同| 久久永久免费人妻精品下载| 久久热这里只有精品在线观看| 亚洲AⅤ优女AV综合久久久| 国产精品美女久久久久av爽 | 欧美日韩成人精品久久久免费看| 国内精品久久久久影院优| 久久夜色精品国产欧美乱| 午夜精品久久久久久久久| 久久亚洲国产成人精品性色|