[Muduo网络库源码分析] (7) base/Singleton.h_SINGLETON对象创建型模式
来源:互联网 发布:mac 恢复icloud 删除 编辑:程序博客网 时间:2024/06/11 07:47
SINGLETON(单件)—对象创建型模式
实现:Singleton类
功能:保证一个类仅有一个实例,并提供一个访问它的全局访问点。
知识点:
- pthread_once
保证init_routine()函数在本进程执行序列中仅执行一次 static用法
Singleton设计模式
用途:
- 当类只能有一个实例而且客户可以从一个众所周知的访问点访问它时。
- 当这个唯一实例应该是通过子类化可扩展的,并且客户应该无需更改代码就能使用一个扩展的实例时。
代码及分析:
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 <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{#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
阅读全文
0 0
- [Muduo网络库源码分析] (7) base/Singleton.h_SINGLETON对象创建型模式
- muduo库源码学习(base)singleton
- [Muduo网络库源码分析] (9) base/Thread.cc_h_CurrentThread_h线程对象
- [Muduo网络库源码分析] (10) base/ThreadPoll_cc_h_线程池
- [Muduo网络库源码分析] (1) base/Atomic.h_原子操作与原子整数
- [Muduo网络库源码分析] (2) base/Condition.cc_h_条件变量操作
- [Muduo网络库源码分析] (3) base/CountDownLatch.cc_h_“倒计时门闩”同步
- [Muduo网络库源码分析] (4) base/Exception_cc_h_带 stack trace 的异常基类
- [Muduo网络库源码分析] (5) base/FileUtil.cc_h_文件操作
- [Muduo网络库源码分析] (6) base/Mutex.h_互斥锁操作
- [Muduo网络库源码分析] (8) base/StringPiece.h_字符串参数传递类型
- [Muduo网络库源码分析] (11) base/Types.h_基本类型声明
- muduo库源码解析:单例模式singleton
- muduo网络库源码分析-定时器
- Singleton-对象创建型模式
- muduo库源码分析
- muduo库源码学习(base)Atomic
- muduo库源码学习(base)AsyncLogging
- Apache Storm 的安装、配置及入门基础(一)
- Java内存模型
- macOS 下安装SDKMAN 软件开发工具包管理器
- 【solr】使用总结
- 2016 ACM/ICPC Asia Regional Qingdao Online(青岛网络赛)
- [Muduo网络库源码分析] (7) base/Singleton.h_SINGLETON对象创建型模式
- 关于使用cookie存储购物车信息存在的问题
- 请键入NET HELPMSG 3534以获得更多的帮助
- ble调制方式
- Zepto源码之Deferred模块
- hdu 3364 Lanterns (高斯消元)
- 欢迎使用CSDN-markdown编辑器
- 【Docker学习】运行Docker时遇到的一些错误
- Zepto源码之Callbacks模块