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代码学习笔记的更多相关文章

  1. Learning Memory-guided Normality代码学习笔记

    Learning Memory-guided Normality代码学习笔记 记忆模块核心 Memory部分的核心在于以下定义Memory类的部分. class Memory(nn.Module): ...

  2. 初探boost之timer库学习笔记

    timer   使用方法     #include <boost/timer.hpp> #include <iostream> using namespace std; usi ...

  3. boost asio io_service学习笔记

    构造函数 构造函数的主要动作就是调用CreateIoCompletionPort创建了一个初始iocp. Dispatch和post的区别 Post一定是PostQueuedCompletionSta ...

  4. DeepLearnToolbox-master代码学习笔记

    卷积神经网络(CNN)博大精深,网上资料浩如烟海,让初学者无从下手.笔者以为,学习编程还是从代码实例入们最好.目前,学习CNN最好的代码实例就是,DeepLearnToolbox-master,不用装 ...

  5. Boost线程库学习笔记

    一.创建一个线程 创建线程 boost::thread myThread(threadFun); 需要注意的是:参数可以是函数对象或者函数指针.并且这个函数无参数,并返回void类型. 当一个thre ...

  6. 初探boost之noncopyable学习笔记

    noncopyable 功能 同意程序轻松实现一个不可复制的类. 需包括头文件 #include<boost/noncopyable.hpp>     或 #include<boos ...

  7. C# 好代码学习笔记(1):文件操作、读取文件、Debug/Trace 类、Conditional条件编译、CLS

    目录 1,文件操作 2,读取文件 3,Debug .Trace类 4,条件编译 5,MethodImpl 特性 5,CLSCompliantAttribute 6,必要时自定义类型别名 目录: 1,文 ...

  8. 1.JAVA中使用JNI调用C++代码学习笔记

    Java 之JNI编程1.什么是JNI? JNI:(Java Natibe Inetrface)缩写. 2.为什么要学习JNI?  Java 是跨平台的语言,但是在有些时候仍然是有需要调用本地代码 ( ...

  9. APM代码学习笔记1

    libraries目录 传感器 AP_InertialSensor 惯性导航传感器 就是陀螺仪加速计 AP_Baro 气压计 居然支持BMP085 在我印象中APM一直用高端的MS5611 AP_Co ...

随机推荐

  1. Unity入门&物理引擎

    一.Unity六大模块 首先,Unity界面有六大模块,分别是:Hierarchy,Scene,Game,Inspector,Project,Console.下面对这六个视图的功能进行详解. 1.Hi ...

  2. JMeter3.0(三十八)图形化HTML报告中文乱码问题处理(转载)

    转载自 http://www.cnblogs.com/yangxia-test 由于个人在JMeter 3.0的实际应用中,脚本中的Test Plan/Sampler等元件命名都没有使用中文,所以在之 ...

  3. V4 V7 V13支持包的区别(转)

    三者均为支持包,可以让低版本系统使用高版本特性,支持最小版本有差异 V4支持1.6以上 V7支持2.1以上 V13支持3.2以上 V7依赖V4 转自:

  4. java面试题:jvm

    jvm内存区域 Q:jvm内存怎么划分的? 答: 方法区(线程共享):各个线程共享的一个区域,用于存储虚拟机加载的类信息.常量.静态变量.即时编译器编译后的代码等数据.虽然 Java 虚拟机规范把方法 ...

  5. java并发:CopyOnWriteArrayList简单理解

    Java集合的快速失败机制 “fail-fast” "fail-fast"是java集合的一种错误检测机制,当多个线程对集合进行结构上的改变的操作时,有可能会产生 fail-fas ...

  6. FileInputStream.FileOutputStream执行图片复制

    /** * 需求:拷贝一个图片 * 思路: * 1.创建一个字符输入流和图片相关联. * 2.用字节写入流对创建图片文件,用于存储到图片数据. * 3.通过循环续写,完成数据的存储. * 4.关闭资源 ...

  7. html position定位

    一.fixed居中 css样式代码:{ position:fixed left: 0; right: 0; margin:0 auto; width:300px } 二.Position属性有四个值: ...

  8. mysql 5.7 基于GTID 主从同步的1236故障处理(其它事务故障等同)

    登录从库 stop slave; 查看执行事务 show slave status\G Retrieved_Gtid_Set:  Executed_Gtid_Set: ee3bdb44-f6a1-11 ...

  9. CentOS 下搭建Hudson

    1.下载Hudson安装包 wget http://ftp.jaist.ac.jp/pub/eclipse/hudson/war/hudson-3.3.3.war 2.执行 java -jar hud ...

  10. goto,void,extern,sizeof分析

    goto: 程序的质量与goto出现的次数成反比,禁用 goto的副作用:破环了程序的结构化的顺序执行的过程,它有可能会跳过程序的应该执行的一些步骤. void: 修饰函数返回值和参数 c语言中没有定 ...