boost timer代码学习笔记
socket连接中需要判断超时
所以这几天看了看boost中计时器的文档和示例
一共有五个例子 从简单的同步等待到异步调用超时处理
先看第一个例子
// timer1.cpp: 定义控制台应用程序的入口点。
// #include "stdafx.h"
#include <iostream>
#include <boost/asio.hpp>
#include <boost/date_time/posix_time/posix_time.hpp> void print() {
std::cout << "Hello world" << std::endl;
} int main()
{
boost::asio::io_service io; boost::asio::deadline_timer t(io,boost::posix_time::seconds());
t.wait(); print(); return ;
}
timer
几行代码 简单的声明计时器 等待五秒后继续执行
但是代码有一个问题就是 等待时间内 流程是卡死的
所以加以改进就是例子2 更改为异步等待
// timer2.cpp: 定义控制台应用程序的入口点。
// #include "stdafx.h"
#include <iostream>
#include <boost/asio.hpp>
#include <boost/date_time/posix_time/posix_time.hpp> void print(const boost::system::error_code&) {
std::cerr << "Hello world!" << std::endl;
} int main()
{
boost::asio::io_service io;
boost::asio::deadline_timer t(io,boost::posix_time::seconds());
t.async_wait(&print);
for (int i = ; i < ; i++) {
std::cout << "wait" << std::endl;
}
io.run(); return ;
}
timer2
例子3又添加了一个内容
异步等待函数中 再次设置异步等待时间和超时异步调用的回调函数
一共5次 由于传入的是计数count变量的指针 所以计数变量会累加
当累加到5 则不再继续
代码如下:
// timer3.cpp: 定义控制台应用程序的入口点。
//
#include <iostream>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/date_time/posix_time/posix_time.hpp> void print(const boost::system::error_code& /*e*/,
boost::asio::deadline_timer* t, int* count)
{
if (*count < )
{
std::cout << *count << std::endl;
++(*count); t->expires_at(t->expires_at() + boost::posix_time::seconds());
t->async_wait(boost::bind(print,
boost::asio::placeholders::error, t, count));
}
} int main()
{
boost::asio::io_service io; int count = ;
boost::asio::deadline_timer t(io, boost::posix_time::seconds());
t.async_wait(boost::bind(print,
boost::asio::placeholders::error, &t, &count)); io.run(); std::cout << "Final count is " << count << std::endl; return ;
}
timer3
例子4同例子3 不同之处在于 以类的形式封装了超时异步回调函数以及timer
// timer4.cpp: 定义控制台应用程序的入口点。
//
#include <iostream>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/date_time/posix_time/posix_time.hpp> class printer
{
public:
printer(boost::asio::io_service& io)
: timer_(io, boost::posix_time::seconds()),
count_()
{
timer_.async_wait(boost::bind(&printer::print, this));
} ~printer()
{
std::cout << "Final count is " << count_ << std::endl;
} void print()
{
if (count_ < )
{
std::cout << count_ << std::endl;
++count_; timer_.expires_at(timer_.expires_at() + boost::posix_time::seconds());
timer_.async_wait(boost::bind(&printer::print, this));
}
} private:
boost::asio::deadline_timer timer_;
int count_;
}; int main()
{
boost::asio::io_service io;
printer p(io);
io.run(); return ;
}
timer4
例子5开启了两个timer分别在不同线程中运行
基本上阅读没有任何问题
但是实际编写中要深入了解 asio::io_service::strand 和 多线程运行ioser.run()
这两点需要注意 可以尝试不适用strand_.wrap
运行代码比对代码运行结果进行理解
代码如下
#include <iostream>
#include <boost/asio.hpp>
#include <boost/thread/thread.hpp>
#include <boost/bind.hpp>
#include <boost/date_time/posix_time/posix_time.hpp> class printer
{
public:
printer(boost::asio::io_service& io)
: strand_(io),
timer1_(io, boost::posix_time::seconds()),
timer2_(io, boost::posix_time::seconds()),
count_()
{
timer1_.async_wait(strand_.wrap(boost::bind(&printer::print1, this)));
timer2_.async_wait(strand_.wrap(boost::bind(&printer::print2, this)));
} ~printer()
{
std::cout << "Final count is " << count_ << std::endl;
} void print1()
{
if (count_ < )
{
std::cout << "Timer 1: " << count_ << std::endl;
++count_; timer1_.expires_at(timer1_.expires_at() + boost::posix_time::seconds());
timer1_.async_wait(strand_.wrap(boost::bind(&printer::print1, this)));
}
} void print2()
{
if (count_ < )
{
std::cout << "Timer 2: " << count_ << std::endl;
++count_; timer2_.expires_at(timer2_.expires_at() + boost::posix_time::seconds());
timer2_.async_wait(strand_.wrap(boost::bind(&printer::print2, this)));
}
} private:
boost::asio::io_service::strand strand_;
boost::asio::deadline_timer timer1_;
boost::asio::deadline_timer timer2_;
int count_;
}; int main()
{
boost::asio::io_service io;
printer p(io);
boost::thread t(boost::bind(&boost::asio::io_service::run, &io));
io.run();
t.join(); return ;
}
timer5
boost timer代码学习笔记的更多相关文章
- Learning Memory-guided Normality代码学习笔记
Learning Memory-guided Normality代码学习笔记 记忆模块核心 Memory部分的核心在于以下定义Memory类的部分. class Memory(nn.Module): ...
- 初探boost之timer库学习笔记
timer 使用方法 #include <boost/timer.hpp> #include <iostream> using namespace std; usi ...
- boost asio io_service学习笔记
构造函数 构造函数的主要动作就是调用CreateIoCompletionPort创建了一个初始iocp. Dispatch和post的区别 Post一定是PostQueuedCompletionSta ...
- DeepLearnToolbox-master代码学习笔记
卷积神经网络(CNN)博大精深,网上资料浩如烟海,让初学者无从下手.笔者以为,学习编程还是从代码实例入们最好.目前,学习CNN最好的代码实例就是,DeepLearnToolbox-master,不用装 ...
- Boost线程库学习笔记
一.创建一个线程 创建线程 boost::thread myThread(threadFun); 需要注意的是:参数可以是函数对象或者函数指针.并且这个函数无参数,并返回void类型. 当一个thre ...
- 初探boost之noncopyable学习笔记
noncopyable 功能 同意程序轻松实现一个不可复制的类. 需包括头文件 #include<boost/noncopyable.hpp> 或 #include<boos ...
- C# 好代码学习笔记(1):文件操作、读取文件、Debug/Trace 类、Conditional条件编译、CLS
目录 1,文件操作 2,读取文件 3,Debug .Trace类 4,条件编译 5,MethodImpl 特性 5,CLSCompliantAttribute 6,必要时自定义类型别名 目录: 1,文 ...
- 1.JAVA中使用JNI调用C++代码学习笔记
Java 之JNI编程1.什么是JNI? JNI:(Java Natibe Inetrface)缩写. 2.为什么要学习JNI? Java 是跨平台的语言,但是在有些时候仍然是有需要调用本地代码 ( ...
- APM代码学习笔记1
libraries目录 传感器 AP_InertialSensor 惯性导航传感器 就是陀螺仪加速计 AP_Baro 气压计 居然支持BMP085 在我印象中APM一直用高端的MS5611 AP_Co ...
随机推荐
- zabbix 利用python脚本实现钉钉告警
Zabbix 利用python脚本实现钉钉告警 1.安装python3.6环境 2.创建python脚本 cd local/zabbix-4.0.3/share/zabbix/alertscripts ...
- linux中awk命令学习
awk是一个强大的文本分析工具,awk就是把文件逐行的读入,以空格为默认分隔符将每行切片,切开的部分再进行各种分析处理. 通常获取某一行时,我们会使用 “grep 关键字“进行查找,但在读取某一列时, ...
- Java,JDK动态代理的原理分析
1. 代理基本概念: 以下是代理概念的百度解释:代理(百度百科) 总之一句话:三个元素,数据--->代理对象--->真实对象:复杂一点的可以理解为五个元素:输入数据--->代理对象- ...
- js中switch/case分支的值可以是变量或表达式
在一些高级语言如C#中,switch分支的值只能是常量,而js中可以是变量或表达式: <!DOCTYPE html> <html lang="en"> &l ...
- 项目没有build path问题(转)
感谢作者分享:https://blog.csdn.net/u012572815/article/details/76353018 问题1.通过eclipse的svn资源库添加的项目,显示的方式和直接创 ...
- 使用 Actuator 监控
参考文章:https://www.jianshu.com/p/ba85f56a2013 Actuator 提供对自身应用的监控.配置查看等. 步骤一:导入actuator 依赖 <depende ...
- 四、Factory Method 工厂方法模式
需求:对象的创建由工厂统一创建,会使用到模板模式 代码清单: 工厂: public abstract class Factory { public abstract Product createPro ...
- 第九章 词典 (e)桶/计数排序
- Python+Selenium学习--前进和后退
场景 这两个功能一般不太常用.所能想到的场景大概也就是在几个页面间来回跳转,省去每次都get url. 代码 #!/usr/bin/env python # -*- coding:utf-8 -*- ...
- CSS3实现图片循环旋转
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...