(七)boost库之单例类
生活随笔
收集整理的這篇文章主要介紹了
(七)boost库之单例类
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
? ?一、boost.serialzation的單件實現
??? 單例模式是一種常用的軟件設計模式。在它的核心結構中只包含一個被稱為單例類的特殊類。通過單例模式可以保證系統中一個類只有一個實例而且該實例易于外界訪問,從而方便對實例個數的控制并節約系統資源。如果希望在系統中某個類的對象只能存在一個,單例模式是最好的解決方案。
??? 單例,通常在一個大型項目中單例是非常常見的,boost庫沒有提供專門的單例類,但可以在其它庫中找到他的實現
#include <boost/serialization/singleton.hpp> class ThreadPoll : public boost::serialization::singleton<ThreadPoll> { };?
當然,你也可以不使用繼承的方式,只需要typedef一下,就可以得到一個全局訪問點,這種方式會更加的靈活。
class ThreadPoll { }; typedef boost::serialization::singleton<ThreadPoll> ThreadPollAgent;?簡單示例:
#include <iostream> #include <boost/serialization/singleton.hpp> class Man { public:int getAge(){return m_age;}void setAge(int pAge){m_age = pAge;}private:int m_age; };typedef boost::serialization::singleton<Man> firstMan;int main() {firstMan::get_mutable_instance().setAge(100);std::cout<< firstMan::get_mutable_instance().getAge() <<std::endl;return 0; }
但是,該單例是非線程安全的,單件類實現了一個全局訪問點,如果在多個線程環境中訪問,是需要自行加鎖的,因此,如果需要一個線程安全的單件,需要自己實現。
?
二、自行實現線程安全單件
/****************************************************** * 支持線程安全的單例類 *******************************************************/ #ifndef __SINGLETON_H__ #define __SINGLETON_H__ #include <boost/noncopyable.hpp> #include <boost/thread.hpp> //哨兵類,負責多線程操作,自動加鎖解鎖 //哨兵類不允許拷貝, template<typename T> class SingletonGuard : boost::mutex::scoped_lock, public boost::noncopyable { public: explicit SingletonGuard(T* inst, boost::mutex& mt):boost::mutex::scoped_lock(mt),m_guardPtr(inst) { } T* operator->() { return m_guardPtr; } private: T* m_guardPtr; }; //監視類,用于監視單例的狀態 template<typename T> class Singleton_wrapper : public T { public: static bool m_is_destroyed; ~Singleton_wrapper(){ m_is_destroyed = true; } }; template<typename T> bool Singleton_wrapper< T >::m_is_destroyed = false; //單例 template<typename T> class Singleton : public boost::noncopyable { public: static SingletonGuard<T> get_mutable_instance(){ return SingletonGuard<T>(&get_instance(), m_signalMutex); } static const T & get_const_instance(){ return get_instance(); } private: static T & instance; static void use(T const &) {} static T & get_instance() { static Singleton_wrapper< T > t; //編譯期進行初始化實例 BOOST_ASSERT(! Singleton_wrapper< T >::m_is_destroyed); use(instance); return static_cast<T &>(t); } static boost::mutex m_signalMutex; protected: boost::mutex::scoped_lock ScopedLock() { return boost::mutex::scoped_lock(m_signalMutex); } }; template<typename T> boost::mutex Singleton< T >::m_signalMutex; template<typename T> T & Singleton< T >::instance = Singleton< T >::get_instance(); #endif //__SINGLETON_H__?
該類參考boost單件的實現,使用上相似,但get_mutable_instance返回的不是實例對象,而是保存該實例指針的一個臨時變量,通過定義->符號訪問實例的函數,離開該作用域時,臨時變量析構,
自行解鎖。通過該單例訪問的所有函數都將已加鎖的形式進行訪問,如果想在不加鎖的情況下訪問,只能通過get_const_instance獲取實例的方式訪問。
你可以像下面這樣使用:
class ThreadPoll { public: void Update(){}; int Query()const{}; }; typedef Singleton<ThreadPoll> ThreadPollAgent; ThreadPollAgent::get_mutable_instance()->Update(); ThreadPollAgent::get_const_instance().Query();總結
以上是生活随笔為你收集整理的(七)boost库之单例类的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: (六)boost库之内存管理shared
- 下一篇: (八)boost库之异常处理