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. Java 动态代理 两种实现方法

    AOP的拦截功能是由java中的动态代理来实现的.说白了,就是在目标类的基础上增加切面逻辑,生成增强的目标类(该切面逻辑或者在目标类函数执行之前,或者目标类函数执行之后,或者在目标类函数抛出异常时候执 ...

  2. python-ceilometerclient命令行(2)

    命令行解析工具argparse argparse是python标准库中的模块,利用argparse,可以完成对命令行的参数定义.解析以及后续的处理.一个简单的例子: # coding:utf-8 im ...

  3. Python爬虫使用MD5加密的坑

    由于公司的业务需要,需要爬取很多的国外网站图片,然后兄弟我一路正则杀过去,总共匹配到658张链接,心里美滋滋开始写下载的代码.然后就有了这次坑的记录. 首先这是我查到的链接数量 然后爬虫跑完后,美滋滋 ...

  4. 【C++】正则表达式引擎学习心得

    最近参照一些资料实现了一个非常简易的正则表达式引擎,支持基本的正则语法 | + * ()等. 实现思路是最基本的:正则式->AST->NFA->DFA. 以下是具体步骤: 一. 正则 ...

  5. ASP.NET 登录验证 ihttpmoudle

    问题: 1.iis版本不同(IIS7.0,应用程序池采用的是集成模式,换成经典模式才起作用.) 在 IIS 7 以下的版本中,应用以下配置: <system.web> <httpMo ...

  6. css3 vw、vh属性详解,以及与%、rem的区别介绍

    最近的项目需求,内嵌电视端显示页面,所以使用到了css3的属性:vw \ vh,遇到一些问题,记录下来 vw.vh是什么? 字面上可以简单理解为,vw (view width),vh(view hei ...

  7. cf-Global Round2-E. Pavel and Triangles

    题目链接:http://codeforces.com/contest/1119/problem/E 题意:给定n个数a[i],分别表示长度为2i-1的木条的数量,问使用这些木条最多能构成多少三角形. ...

  8. x86寄存器总结

    X86寄存器 ·x86寄存器分类: 8个通用寄存器:EAX.EBX.ECX.EDX.ESI.EDI.ESP.EBP 1个标志寄存器:EFLAGS 6个段寄存器:CS.DS.ES.FS.GS.SS 5个 ...

  9. c++:空构造空析构的益处之一

    项目开发过程中发现一个问题,有段代码,一个动态库的cpp代码里不包含头文件中类的空构造空析构实现,就会出现编译出的动态库依赖项少了很多.而添加后则多了好几个依赖项.下面看例子: ##a.h class ...

  10. 取得<asp:TextBox中的值:

     取得<asp:TextBox中的值:  var a= document.getElementById("<%= (ID名).ClientID %>").valu ...