muduo之Singleton
生活随笔
收集整理的這篇文章主要介紹了
muduo之Singleton
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
? ? ? ? ?muduo中的單例模式
Singleton.h
// 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 "muduo/base/noncopyable.h"#include <assert.h> #include <pthread.h> #include <stdlib.h> // atexitnamespace muduo {namespace detail { // This doesn't detect inherited member functions! // http://stackoverflow.com/questions/1966362/sfinae-to-check-for-inherited-member-functions template<typename T> struct has_no_destroy {//decltype選擇并返回操作數的數據類型template <typename C> static char test(decltype(&C::no_destroy));template <typename C> static int32_t test(...);const static bool value = sizeof(test<T>(0)) == 1;//判斷如果是類的話,是否有no_destroy方法 }; } // namespace detailtemplate<typename T> class Singleton : noncopyable {public:Singleton() = delete;~Singleton() = delete;static T& instance() //static,保證可以通過類作用域運算符進行調用{//pthread_once()函數,在多線程中,保證某個函數只被執行一次。<pthread.h>pthread_once(&ponce_, &Singleton::init); assert(value_ != NULL);return *value_;}private:static void init(){value_ = new T(); //根據傳入的類型進行newif (!detail::has_no_destroy<T>::value)//當參數是類且沒有"no_destroy"方法才會注冊atexit的destroy{//注冊一個函數,在程序終止時執行::atexit(destroy);//登記atexit時調用的銷毀函數,防止內存泄漏}}static void destroy(){用typedef定義了一個數組類型,數組的大小不能為-1,利用這個方法,如果是不完全類型,編譯階段就會發現錯誤定義一個char數組類型 T_MUST_BE_COMPELET_TYPE :char[-1]---如果T只聲明沒有定義-不完全類型 ; char[1]--T是完全類型,即有定義,delete操作,可以調用析構函數,沒定義就沒有析構函數,delete就不會調用析構函數了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>//創建一個全局的Singleton<T>::ponce_變量,體現static的作用 pthread_once_t Singleton<T>::ponce_ = PTHREAD_ONCE_INIT; //ponce_初始化template<typename T> T* Singleton<T>::value_ = NULL;} // namespace muduo#endif // MUDUO_BASE_SINGLETON_H?
總結
以上是生活随笔為你收集整理的muduo之Singleton的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: muduo之CountDownLatch
- 下一篇: muduo之Atomic