一、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. 【转载】MySQL查询当天0点,昨天时间

    转载自:https://blog.csdn.net/qq_22158021/article/details/78800299 今天是 SELECT NOW();-- 2015-09-28 13:48: ...

  2. 2018-2-13-win10-UWP-应用设置

    title author date CreateTime categories win10 UWP 应用设置 lindexi 2018-2-13 17:23:3 +0800 2018-2-13 17: ...

  3. 作业(二)—python实现wc命令

    Gitee地址:https://gitee.com/c1e4r/word-count(为什么老师不让我们用github) 0x00 前言 好久没发博客了,感觉自己的学习是有点偷懒了.这篇博客也是应专业 ...

  4. nginx 反向代理服务

    目录 Nginx代理服务基本概述 Nginx代理服务常见模式 Nginx代理服务支持协议 Nginx反向代理配置语法 Nginx反向代理场景实践 配置代理实战 在lb01上安装nginx Nginx代 ...

  5. go语言从例子开始之Example18.struct结构体

    Go 的结构体 是各个字段字段的类型的集合.这在组织数据时非常有用 Example: package main import "fmt" type product struct{ ...

  6. 解读dbcp自动重连那些事(转)

    转自:http://agapple.iteye.com/blog/791943 Hi all : 最近在做 offerdetail 优化时,替换了数据库驱动,从 c3p0 0.9.1 -> db ...

  7. @InitBinder 前端传递date时间类型属性时,转换错误问题

    在Controller里加上这段代码 @InitBinder public void initBinder(WebDataBinder binder) { binder.registerCustomE ...

  8. VSCode中行数与代码之间用点点点代替

    在settings.json中添加一行 "editor.renderWhitespace": "all"

  9. 硬盘监控和分析工具:Smartctl

    https://linux.cn/article-4682-1.html Smartctl(S.M.A.R.T 自监控,分析和报告技术)是类Unix系统下实施SMART任务命令行套件或工具,它用于打印 ...

  10. linux编译php

    ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mysql=/usr/loc ...