一、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. Spring开发总结

    自动装载配置开启: spring.factories内容: org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.b ...

  2. JavaSE---Runtime类

    1.概述 1.1 Runtime类  代表 java程序运行时环境: 1.2 Runtime类  提供的类方法: getRuntime():获取Runtime实例: gc():通知垃圾回收器回收资源: ...

  3. python3 线程调用与GIL 锁机制

    转载

  4. 【leetcode】935. Knight Dialer

    题目如下: A chess knight can move as indicated in the chess diagram below:  .            This time, we p ...

  5. js学习笔记-日期对象

    <body> <script> var d = new Date() console.log(d) var arr = ['星期日', '星期一', '星期二', '星期三', ...

  6. 管理Session

    1:把session和本地线程绑定在一起. 1):创建一个sessionFactory.然后由它去创建session package com.hq.util; import org.hibernate ...

  7. PHP disk_total_space() 函数

    定义和用法 disk_total_space() 函数返回指定目录的磁盘总容量,以字节为单位. 语法 disk_total_space(directory) 参数 描述 directory 必需.规定 ...

  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. SQL必知必会——插入数据(十五)

    1.数据插入 INSERT用来将行插入(或添加)到数据库表.插入有几种方式: 插入完整的行插入行的一些部分插入某些查询的结果注意:1.使用INSERT语句可能需要客户端/服务端DBMS中的特定安全权限 ...

  10. 筆記本 wifi走外网线 網卡走內網

    筆記本 wifi走外网线  網卡走內網 ,案列 -------------------------------------------------------- route print        ...