muduo库的Singleton类剖析

来源:互联网 发布:50知天命60耳顺 编辑:程序博客网 时间:2024/05/17 07:52


如题,分析muduo库线程安全的单例模式类。

它的类图如下:



分析如下:

#ifndef MUDUO_BASE_SINGLETON_H#define MUDUO_BASE_SINGLETON_H#include <boost/noncopyable.hpp>#include <assert.h>#include <stdlib.h> // atexit#include <pthread.h>namespace muduo{namespace detail{//不能侦测继承的成员函数// This doesn't detect inherited member functions!// http://stackoverflow.com/questions/1966362/sfinae-to-check-for-inherited-member-functionstemplate<typename T>struct has_no_destroy      {  template <typename C> static char test(typeof(&C::no_destroy)); // or decltype in C++11  template <typename C> static int32_t test(...);  const static bool value = sizeof(test<T>(0)) == 1;    //判断如果是类的话,是否有no_destroy方法。};}template<typename T>class Singleton : boost::noncopyable{ public:  static T& instance()   //得到对象  {    pthread_once(&ponce_, &Singleton::init);   //第一次调用会在init函数内部创建,pthread_once保证该函数只被调用一次!!!!       //并且pthread_once()能保证线程安全,效率高于mutex    assert(value_ != NULL);    return *value_;    //利用pthread_once只构造一次对象  } private:  Singleton();  ~Singleton();  static void init()   //客户端初始化该类  {    value_ = new T();   //直接调用构造函数    if (!detail::has_no_destroy<T>::value)   //当参数是类且没有"no_destroy"方法才会注册atexit的destroy    {      ::atexit(destroy);   //登记atexit时调用的销毁函数,防止内存泄漏    }  }  static void destroy()  //程序结束后自动调用该函数销毁  {    //用typedef定义了一个数组类型,数组的大小不能为-1,利用这个方法,如果是不完全类型,编译阶段就会发现错误    typedef char T_must_be_complete_type[sizeof(T) == 0 ? -1 : 1];  //要销毁这个类型,这个类型必须是完全类型    T_must_be_complete_type dummy; (void) dummy;  //这个    delete value_;   //销毁    value_ = NULL;   //赋空  } private:  static pthread_once_t ponce_;     //pthread_once的参数  static T*             value_;        //模板T类型的指针};template<typename T>pthread_once_t Singleton<T>::ponce_ = PTHREAD_ONCE_INIT;   //初始化pthread_oncetemplate<typename T>T* Singleton<T>::value_ = NULL;    //静态成员外部会初始化为空}#endif

类内部用了C++的SFINAE技术,我在另外一篇博客中有介绍:http://blog.csdn.net/freeelinux/article/details/53429009


muduo的单例模式采用模板类实现,它内部维护一个模板参数的指针,可以生成任何一个模板参数的单例。凭借SFINAE技术muduo库可以检测模板参数如果是类的话,并且该类注册了一个no_destroy()方法,那么muduo库不会去自动销毁它。否则muduo库会在init时,利用pthread_once()函数为模板参数,注册一个atexit时的destroy()垃圾回收方法,实现自动垃圾回收。智能指针也能达到类似的效果,我们平时写的单例模式在Singleton中写一个Garbage类也可以完成垃圾回收。

muduo库与我们平时使用mutex取get_instance不同,我们平时通常在get_Instance中只产生对象,在此之前需要先手动调用init()方法。但muduo库使用了pthread_once()函数,该函数只会执行一次,且是线程安全的函数,所以只有在我们第一次get_instance()时,才会自动调用Init()方法。此后只会获取实例。

不得不说,开了眼界,原来单例模式还可以这样写。

0 0
原创粉丝点击