关于Muduo网络库中Singleton代码的疑问

46 views
Skip to first unread message

chudong...@gmail.com

unread,
Jul 30, 2017, 11:32:50 PM7/30/17
to 西邮Linux兴趣小组
// Use of this source code is governed by a BSD-style license
// that can be found in the License file.
//
// Author: Shuo Chen (chenshuo at chenshuo dot com)

#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!
template<typename T>
struct has_no_destroy
{
#ifdef __GXX_EXPERIMENTAL_CXX0X__
  template <typename C> static char test(decltype(&C::no_destroy));
#else
  template <typename C> static char test(typeof(&C::no_destroy));
#endif
  template <typename C> static int32_t test(...);
  const static bool value = sizeof(test<T>(0)) == 1;
};
}

template<typename T>
class Singleton : boost::noncopyable
{
 public:
  static T& instance()
  {
    //保证init_routine()函数在本进程执行序列中仅执行一次。
    pthread_once(&ponce_, &Singleton::init);
    assert(value_ != NULL);
    return *value_;
  }

 private:
  //默认构造函数
  Singleton();
  //默认析构函数
  ~Singleton();

  static void init()
  {
    //初始化value_,并把其清零
    value_ = new T();
    if (!detail::has_no_destroy<T>::value)
    {
      //在进程退出时回收资源
      ::atexit(destroy);
    }
  }
  //回收资源
  static void destroy()
  {
    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:
  //pthread_once参数
  static pthread_once_t ponce_;
  //指向一个实例
  static T*             value_;
};

//static需要在类外进行初始化
template<typename T>
pthread_once_t Singleton<T>::ponce_ = PTHREAD_ONCE_INIT;

template<typename T>
T* Singleton<T>::value_ = NULL;

}
#endif




想问一下学长学姐下面这段代码的用法
typedef char T_must_be_complete_type[sizeof(T) == 0 ? -1 : 1];
T_must_be_complete_type dummy; (void) dummy;

Reply all
Reply to author
Forward
0 new messages