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 ...
随机推荐
- springboot 线程池
我们常用ThreadPoolExecutor提供的线程池服务,springboot框架提供了@Async注解,帮助我们更方便的将业务逻辑提交到线程池中异步执行,今天我们就来实战体验这个线程池服务: 本 ...
- js中基本事件的总结,onclick、onblur、onchange等
js中的基本事件总结: 特定的场景下发生的一个动作:事件:事件=函数(),事件发生会触发函数执行. 属性 当以下情况发生时,出现此事件 FF N IE onabort 图像加载被中断 1 3 4 on ...
- SVN Commit:将本地代码更新到服务器代码
1.点击客户端“TortoiseSVN” 选中后显示: 点击Import: 点击“ok”:
- 字符串相似度算法(编辑距离Levenshtein Distance)的应用场景
应用场景 DNA分析: 将DNA的一级序列如β-球蛋白基因的第一个外显子(Exon)转化为分子“结构图”,然后由所得“结构图”提取图的不变量,如分子连接性指数.以图的不变量作为自变量,再由相似度计算公 ...
- hadoop深入简出(二)
1.上传文件 Hadoop fs -put hello.txt / 2.查看上传的文件 hadoop fs -ls / hadoop fs -text /hello.txt 两个命令都可以 3.创建文 ...
- 在.NET 4中用IIS部署WCF就这么简单
在.NET 3.5中,我们需要这样做: 1. 添加一个HelloService.svc文件,添加ServiceHost标记,在Service中添加WCF服务实现的名称,比如: <%@ Servi ...
- chrome谷歌浏览器常用快捷键搜集整理
搜集了下面比较实用的快捷键,部分不好操作的组合键就不写了:Ctrl+N:打开新窗口. Ctrl+T:打开新标签页.Ctrl+W:关闭当前标签Alt+F4:关闭chrome浏览器Ctrl+Tab:切换到 ...
- vue中使用vue-router切换页面时滚动条自动滚动到顶部的方法
原文:http://www.jb51.net/article/129270.htm main.js入口文件配合vue-router写这个 router.afterEach((to,from,next) ...
- iOS开发时使用的bundle路径
bundle是一个目录,其中包含了程序会使用到的资源. 这些资源包含了如图像,声音,编译好的代码,nib文件(用户也会把bundle称为plug-in). 对应bundle,cocoa提供了类NSBu ...
- Inno Setup安装程序单例运行
1.源起: KV项目下载底层升级包,老是报出升级文件占用问题,反复分析,不得其所. 今天突然发现同时启动多个升级程序实例,分析认为安装包同时被调用多次,引发实例访问文件冲突,导致此问题. 安装程序由I ...