// excerpts from http://code.google.com/p/muduo/ // // Use of this source code is governed by a BSD-style license // that can be found in the License file. // // Author: Shuo Chen (giantchen at gmail dot com) #ifndef MUDUO_BASE_SINGLETON_H #define MUDUO_BASE_SINGLETON_H #include #include #include // atexit namespace muduo { template class Singleton : boost::noncopyable { public: static T& instance() { pthread_once(&ponce_, &Singleton::init); return *value_; } private: Singleton(); ~Singleton(); static void init() { value_ = new T(); ::atexit(destroy); } static void destroy() { delete value_; } private: static pthread_once_t ponce_; static T* value_; }; template pthread_once_t Singleton::ponce_ = PTHREAD_ONCE_INIT; template T* Singleton::value_ = NULL; } #endif