17muduo_base库源码分析(八)
来源:互联网 发布:软件维护工程师 编辑:程序博客网 时间:2024/06/05 10:23
1.Singleton类图
线程安全Singleton类实现
(1)pthread_once
(2)atexit
(3)typedef char T_must_be_complete_type[sizeof(T) == 0 ? -1 : 1];
2.代码
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 <pthread.h>#include <stdlib.h> // atexitnamespace muduo{template<typename T>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() { typedef char T_must_be_complete_type[sizeof(T) == 0 ? -1 : 1]; delete value_; } private: static pthread_once_t ponce_; static T* value_;};template<typename T>pthread_once_t Singleton<T>::ponce_ = PTHREAD_ONCE_INIT;template<typename T>T* Singleton<T>::value_ = NULL;}#endifSingleton_test.cc
#include <muduo/base/Singleton.h>#include <muduo/base/CurrentThread.h>#include <muduo/base/Thread.h>#include <boost/noncopyable.hpp>#include <stdio.h>class Test : boost::noncopyable{ public: Test() { printf("tid=%d, constructing %p\n", muduo::CurrentThread::tid(), this); } ~Test() { printf("tid=%d, destructing %p %s\n", muduo::CurrentThread::tid(), this, name_.c_str()); } const muduo::string& name() const { return name_; } void setName(const muduo::string& n) { name_ = n; } private: muduo::string name_;};void threadFunc(){ printf("tid=%d, %p name=%s\n", muduo::CurrentThread::tid(), &muduo::Singleton<Test>::instance(), muduo::Singleton<Test>::instance().name().c_str()); muduo::Singleton<Test>::instance().setName("only one, changed");}int main(){ muduo::Singleton<Test>::instance().setName("only one"); muduo::Thread t1(threadFunc); t1.start(); t1.join(); printf("tid=%d, %p name=%s\n", muduo::CurrentThread::tid(), &muduo::Singleton<Test>::instance(), muduo::Singleton<Test>::instance().name().c_str());}
运行结果
阅读全文
1 0
- 17muduo_base库源码分析(八)
- 11muduo_base库源码分析(二)
- 12muduo_base库源码分析(三)
- 13muduo_base库源码分析(四)
- 15muduo_base库源码分析(六)
- 16muduo_base库源码分析(七)
- 18muduo_base库源码分析(九)
- 19muduo_base库源码分析(十)
- 20muduo_base库源码分析(十一)
- muduo_base 源码分析:Timestamp
- muduo_base 源码分析:AtomicIntegerT
- 高并发服务器架构笔记(3)——muduo_base 源码分析
- 高并发服务器架构笔记(3)——muduo_base 源码分析
- 32muduo_net库源码分析(八)
- DispatcherServlet 源码分析(八)
- mina源码分析八(转)
- Netty5源码分析(八) -- 总结
- contiki 源码分析之其他(八)
- 网络管理(一)
- 1249. 三角形
- 《哞知》——我的第一个软件
- DBUtils学习----BeanProcessor类
- ES6 — Symbol
- 17muduo_base库源码分析(八)
- 不愿看到Java开发者再做的10件事
- OpenJudge Pell数列
- 题目1038:Sum of Factorials
- poj3241 曼哈顿最小生成树
- Friend-Graph
- Intellij IDEA创建maven项目无java文件问题
- 关于安卓内存溢出
- bootstarp-switch根据查询值修改初始显示状态