一、Timestamp类 

1、类图如下:

2、  知识点

(1)     这个类继承了 muduo::copyable, 以及 boost::less_than_comparable.

(2)     boost::less_than_comparable 这个类要求实现 <, 可以自动实现 >, <=, >= (自动推导出来的,模板元的思想)

3、 学习流程
(1) 剥离代码 (在 ~/muduo_practice 下), 编译出来一个只有TimeStamp的base库
(2) 小的测试程序在 ~/muduo_practice/tests 下
    a. Bsa.cc 测试boost编译时断言的 -> BOOST_STATIC_ASSERT      

 #include <boost/static_assert.hpp>

 class Timestamp
{
private:
int64_t microseconds;
}; BOOST_STATIC_ASSERT(sizeof(Timestamp) == sizeof(int64_t));
//BOOST_STATIC_ASSERT(sizeof(short) == sizeof(int64_t));
int main() {
return ;
}

bsa.cc

    b. 测试PRId64

 #include <cstdio>
#include <ctime>
#define _STDC_FORMAT_MACROS
#include <inttypes.h>
#undef _STDC_FORMAT_MACROS int main(void) {
int64_t value = time(NULL);
printf("%"PRId64"\n", value);
return ;
}

prid.cc

    c. 设计一个合理的对TimeStamp类的benchmark
    用一个vector数组存储Timestamp, 用Timestamp的微秒表示计算相邻两个时间戳的时间差。Vector需要提前reserve空间,避免内存开销影响benchmark。
    代码见Timestamp_sara.cc

 //2017-10-29
//Add by wyzhang
//Learn Muduo -- test Timestamp #include <muduo/base/Timestamp.h>
#include <cstdio>
#include <vector>
#define _STDC_FROMAT_MACROS
#include <inttypes.h>
#undef _STDC_FORMAT_MACROS using namespace muduo; // design a benchmark function to test class Timestamp
// we can use a vector to record Timestamp, and calculate difference of neighbors
void benchmark() {
const int kNumbers = * ;
std::vector<Timestamp> vecTimestamp;
vecTimestamp.reserve(kNumbers); //must preReserve. in case calculate the time of allocate mem
for (int i = ; i < kNumbers ; ++i ) {
vecTimestamp.push_back(Timestamp::now());
} int gap[] = {};
Timestamp start = vecTimestamp.front();
for (int i = ; i < kNumbers; ++i ) {
Timestamp next = vecTimestamp[i];
int64_t calGap = next.microSecondsSinceEpoch() - start.microSecondsSinceEpoch(); // use microSeconds here
start = next;
if(calGap < ) {
printf("calGap < 0\n");
} else if (calGap < ){
gap[calGap]++;
} else {
printf("bigGap. [%"PRId64"]\n", calGap);
}
}
for (int i = ; i < ; ++i) {
printf("%d: %d\n", i, gap[i]);
}
} int main() {
//[1] test print timestamp
Timestamp ts(Timestamp::now());
printf("print now = %s\n", ts.toString().c_str());
sleep();
Timestamp ts2 = Timestamp::now();
printf("ts2 = %s\n", ts2.toString().c_str());
double difftime = timeDifference(ts2, ts);
printf("difftime: %f\n", difftime);
//[2] run benchmark
benchmark();
return ;
}

timestamp_sara.cc

执行结果截图如下:没有截全

二、Exception类 

1、类图如下:

2、  知识点

(1) 系统调用:backtrace, 栈回溯,保存各个栈帧的地址

(2) 系统调用:backtrace_symbols, 根据地址,转成相应的函数符号

(3) abi::_cxa_demangle

(4) 抛出了异常,如果不catch,会core

3、 学习流程

(1) 测试程序如下

 #include <cstdio>
#include <muduo/base/Exception.h> class Bar
{
public:
void test() {
throw muduo::Exception("omg oops");
}
}; void foo() {
Bar b;
b.test();
} int main() {
/* 只抛出异常,不捕获,会core */
//foo();
try {
foo();
}
catch (muduo::Exception& ex) {
printf("%s\n", ex.what());
printf("\n");
printf("%s\n", ex.stackTrace());
}
return ;
}

Exception_sara.cc

运行结果如下

【Muduo库】【base】基本类的更多相关文章

  1. muduo库的简单使用-echo服务的编写

    muduo库的简单使用 muduo是一个基于事件驱动的非阻塞网络库,采用C++和Boost库编写. 它的使用方法很简单,参考这篇文章:TCP网络编程本质论 里面有这么几句: 我认为,TCP 网络编程最 ...

  2. muduo库整体架构简析

    muduo是一个高质量的Reactor网络库,采用one loop per thread + thread loop架构实现,代码简洁,逻辑清晰,是学习网络编程的很好的典范. muduo的代码分为两部 ...

  3. muduo库安装

    一.简介 Muduo(木铎)是基于 Reactor 模式的网络库. 二.安装 从github库下载源码安装:https://github.com/chenshuo/muduo muduo依赖了很多的库 ...

  4. Debian8 下面 muduo库编译与使用

    其实<Linux 多线程服务端编程>已经写得很详细 但是考虑到代码版本的更新和操作系统的不同 可能部分位置会有些许出入 这里做个记录 方便以后学习运行 我使用的虚拟 安装的是debian系 ...

  5. muduo库源码剖析(一) reactor模式

    一. Reactor模式简介 Reactor释义“反应堆”,是一种事件驱动机制.和普通函数调用的不同之处在于:应用程序不是主动的调用某个API完成处理,而是恰恰相反,Reactor逆置了事件处理流程, ...

  6. muduo库源码剖析(二) 服务端

    一. TcpServer类: 管理所有的TCP客户连接,TcpServer供用户直接使用,生命期由用户直接控制.用户只需设置好相应的回调函数(如消息处理messageCallback)然后TcpSer ...

  7. muduo网络库源码学习————日志滚动

    muduo库里面的实现日志滚动有两种条件,一种是日志文件大小达到预设值,另一种是时间到达超过当天.滚动日志类的文件是LogFile.cc ,LogFile.h 代码如下: LogFile.cc #in ...

  8. muduo网络库源码学习————日志类封装

    muduo库里面的日志使方法如下 这里定义了一个宏 #define LOG_INFO if (muduo::Logger::logLevel() <= muduo::Logger::INFO) ...

  9. muduo网络库源码学习————线程本地单例类封装

    muduo库中线程本地单例类封装代码是ThreadLocalSingleton.h 如下所示: //线程本地单例类封装 // Use of this source code is governed b ...

随机推荐

  1. python中函数用法

    unique() numpy.tolist() collections.defaultdict() random.sample()[] 1. unique():返回参数数组中所有不同的值,并按照从小到 ...

  2. 用私有构造器或者枚举类型强化SingleTon(单例)属性

    单例(singleton)就是一个只实例化一次的类.使类成为单例可能会使它的测试变得困难,因为除非它实现了作为其类型的接口,否则不可能用模拟实现来代替这个单例.下面是几种实现单例的方法: 1.共有静态 ...

  3. SpringBoot 中定时执行注解(@Scheduled、@EnableScheduling)

    项目开发中经常需要执行一些定时任务,比如需要在每天凌晨时候,分析一次前一天的日志信息.Spring为我们提供了异步执行任务调度的方式,提供TaskExecutor .TaskScheduler 接口. ...

  4. C盘Administrator中 .m2/repository里面是什么

    ${user.home}/.m2/repository文件夹是maven默认的本地仓库地址maven仓库分为远程仓库和本地仓库,当你在pom里配置依赖项目后,maven首先会从本地仓库查找该项目,如果 ...

  5. docker 运行jenkins及vue项目与springboot项目(五.jenkins打包springboot服务且在docker中运行)

    docker 运行jenkins及vue项目与springboot项目: 一.安装docker 二.docker运行jenkins为自动打包运行做准备 三.jenkins的使用及自动打包vue项目 四 ...

  6. 【纪中集训】2019.08.10【省选组】模拟TJ

    前言 一套码农题-- T1 Description 给定一棵\(n(\in[2,10^5])\)个点的树,\(m(≤10^5)\)次询问,每次询问有两个不相同的点,要让所有点走到这两个点之一(走一条边 ...

  7. Ceph BlueStore与FileStore:利用Micron NVMe SSD进行性能比较

    https://www.micron.com/about/blog/2018/may/ceph-bluestore-vs-filestoreblock-performance-comparison-w ...

  8. POJ 1655 Balancing Act (树状dp入门)

    Description Consider a tree T with N (1 <= N <= 20,000) nodes numbered 1...N. Deleting any nod ...

  9. 工程师技术(三):独立Web站点的快速部署、虚拟Web主机的部署、配置网页内容访问、使用自定Web根目录、配置安全Web服务、部署并测试WSGI站点

    一.独立Web站点的快速部署 目标: 本例要求为 http://server0.example.com 配置Web站点,要求如下: 1> 从http://classroom/pub/materi ...

  10. 自己动手写ORB特征

    通过一些简单的算法修改,使ORB的提取效率加速了5.8倍.编译该程序需要CPU支持SSE指令集. 如果我们能够对特征提取部分进一步并行化处理,则算法还可以有加速的空间. // // Created b ...