一、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. translation of 《deep learning》 Chapter 1 Introduction

    原文: http://www.deeplearningbook.org/contents/intro.html Inventors have long dreamed of creating mach ...

  2. Fiji-imageJ 无法打开

    可能的原因是文件的路径包含中文名称.

  3. FTPClient TLS 与 FTP 进行数据传输异常:Remote host closed connection during handshake

    环境:java JDK 1.8.org.apache.commons-net-3.6.jar.端口已放开 FTPClient ftpClient = new FTPClient(protocol, f ...

  4. 给元素绑定 class

    <div id="app04"> <label v-bind:class="{'Class1':Class1}">sasjadjagd& ...

  5. 【Http】队头阻塞(Head of line blocking)多路复用(Multiplexing)

        图中第一种请求方式,就是单次发送request请求,收到response后再进行下一次请求,显示是很低效的. 于是http1.1提出了管线化(pipelining)技术,就是如图中第二中请求方 ...

  6. spring boot2.x集成spring security5与druid1.1.13(一)

    版本:         spring boot 2.1.2.RELEASE         druid-spring-boot-starter 1.1.13 步骤:        一.maven    ...

  7. 【运维】使用Serv-U搭建FTP服务器

    1.先安装好Serv-U,并作为系统服务安装 2.打开Serv-U,新建一个域 3.添加用户 4.解决阿里云专有网络的一个问题 遇到一个情景:需要使用Serv-U进行FTP更新软件,其中使用PASV的 ...

  8. 为什么NULL能多次free

    void __cdecl _free_base (void * pBlock) {           int retval = 0;             if (pBlock == NULL) ...

  9. js中文首字母数组排序

    js中文首字母数组排序 数组的排序js算法: var Pinyin = (function() { var Pinyin = function(ops) { this.initialize(ops); ...

  10. Python基础-main

    Python基础-_main_ 写在前面 如非特别说明,下文均基于Python3 一.__main__的官方解释 参考 _main_ -- Top-level script environment ' ...