// 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_THREADLOCALSINGLETON_H #define MUDUO_BASE_THREADLOCALSINGLETON_H #include namespace muduo { template class ThreadLocalSingleton : boost::noncopyable { public: static T& instance() { if (!t_value_) { t_value_ = new T(); } return *t_value_; } // no way to auto delete it static void destroy() { delete t_value_; t_value_ = 0; } private: static __thread T* t_value_; }; template __thread T* ThreadLocalSingleton::t_value_ = 0; } #endif