muduo库源码学习
缱绻的西格玛
西格玛不离散
展开
-
muduo网络库源码学习————Exception类
Exception类是为异常捕获而设计,可以获得异常的信息以及栈的回溯信息 (原来的代码没有demangle成员函数,输出的格式比较难看,加了demangle成员函数,利用demangle成员函数可以转换格式,使得输出的格式更加接近我们的习惯) 以下的代码是加入了demangle成员函数后的: Exception.h// Use of this source code is governed原创 2015-08-28 20:35:49 · 680 阅读 · 0 评论 -
muduo网络库源码学习————线程池实现
muduo库里面的线程池是固定线程池,即创建的线程池里面的线程个数是一定的,不是动态的。线程池里面一般要包含线程队列还有任务队列,外部程序将任务存放到线程池的任务队列中,线程池中的线程队列执行任务,也是一种生产者和消费者模型。muduo库中的线程池源码如下: 线程池头文件ThreadPool.h//线程池// Use of this source code is governed by a BS原创 2015-08-29 18:17:13 · 2655 阅读 · 0 评论 -
muduo网络库源码学习————无界队列和有界队列
muduo库里实现了两个队列模板类:无界队列为BlockingQueue.h,有界队列为BoundedBlockingQueue.h,两个测试程序实现了生产者和消费者模型。(这里以无界队列为例,有界队列和无界的差不多)代码如下: BlockingQueue.h#include <muduo/base/Condition.h>#include <muduo/base/Mutex.h>#includ原创 2015-08-29 17:22:20 · 2127 阅读 · 0 评论 -
muduo网络库源码学习————线程特定数据
muduo库线程特定数据源码文件为ThreadLocal.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)#ifn原创 2015-08-29 21:34:05 · 546 阅读 · 0 评论 -
muduo网络库源码学习————互斥锁
muduo源码的互斥锁源码位于muduo/base,Mutex.h,进行了两个类的封装,在实际的使用中更常使用MutexLockGuard类,因为该类可以在析构函数中自动解锁,避免了某些情况忘记解锁。代码如下所示:// Use of this source code is governed by a BSD-style license// that can be found in the Lice原创 2015-08-29 11:17:47 · 817 阅读 · 0 评论 -
muduo网络库源码学习————日志类封装
muduo库里面的日志使方法如下这里定义了一个宏#define LOG_INFO if (muduo::Logger::logLevel() <= muduo::Logger::INFO) \ muduo::Logger(__FILE__, __LINE__).stream()返回的stream重载了一系列的运算符,使用方法如下LOG_INFO<<“info ...”; // 使用方式原创 2015-08-30 17:24:31 · 1300 阅读 · 0 评论 -
muduo网络库源码学习————条件变量
muduo里的CountDownLatch类实际上是对条件变量condition进行的封装,既可以用于所有子线程等待主线程发起 “起跑” ,也可以用于主线程等待子线程初始化完毕才开始工作。 condition.h代码如下:// Use of this source code is governed by a BSD-style license// that can be found in the原创 2015-08-29 15:23:49 · 665 阅读 · 0 评论 -
muduo网络库源码学习————日志滚动
muduo库里面的实现日志滚动有两种条件,一种是日志文件大小达到预设值,另一种是时间到达超过当天。滚动日志类的文件是LogFile.cc ,LogFile.h 代码如下: LogFile.cc#include <muduo/base/LogFile.h>#include <muduo/base/Logging.h> // strerror_tl#include <muduo/base/Pro原创 2015-08-30 22:11:09 · 1953 阅读 · 0 评论 -
muduo网络库源码学习————线程本地单例类封装
muduo库中线程本地单例类封装代码是ThreadLocalSingleton.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原创 2015-08-29 23:20:48 · 636 阅读 · 0 评论 -
muduo网络库源码学习————线程类
muduo库里面的线程类是使用基于对象的编程思想,源码目录为muduo/base,如下所示: 线程类头文件:// 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 chens原创 2015-08-29 10:11:23 · 1025 阅读 · 0 评论 -
muduo网络库源码学习————原子性操作Atomic.h
原子性操作可以做到比互斥锁更小的开销,在多线程编程中原子性操作是非常有用的。Atomic.h文件位于muduo/base下,代码如下:// Use of this source code is governed by a BSD-style license// that can be found in the License file.//// Author: Shuo Chen (chens原创 2015-08-28 19:12:28 · 899 阅读 · 0 评论 -
muduo网络库源码学习————Timestamp.cc
今天开始学习陈硕先生的muduo网络库,moduo网络库得到很多好评,陈硕先生自己也说核心代码不超过5000行,所以我觉得有必要拿过来好好学习下,学习的时候在源码上面添加一些自己的注释,方便日后理解,首先看看UTC时间戳,源码目录为base文件夹: Timestamp.h//UTC时间戳 //类声明文件#ifndef MUDUO_BASE_TIMESTAMP_H#define MUDUO_B原创 2015-08-28 16:37:56 · 1441 阅读 · 0 评论 -
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)原创 2015-08-29 20:32:55 · 671 阅读 · 0 评论