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. spingboot @EnableScheduling

    springboot让开发更简单!springmvc中启用定时任务还得需要在xml中进行配置启用并且要配置扫描器,但是springboot只需要一个注解就可以. @EnableScheduling 无 ...

  2. easymock单元测试跟踪工具

    EasyMock can save a lot of legwork and make unit tests a lot faster to write. builder.com Java E-New ...

  3. Python 继承与多继承

    相关知识点: __class__.__name__的用法. >>> class ABC: def func(self): print('打印类名:',__class__.__name ...

  4. 使用Fiddler远程抓包(转载)

    转载自 http://www.cnblogs.com/111testing/p/6436372.html Fiddler简介以及web抓包 一.Fiddler简介 简单来说,Fiddler是一个htt ...

  5. python--第七天总结

    引言 面向过程:根据业务逻辑从上到下写垒代码 函数式:将某功能代码封装到函数中,日后便无需重复编写,仅调用函数即可 面向对象:对函数进行分类和封装,让开发“更快更好更强...” [面向对象编程(Obj ...

  6. java使用正则表达式

    package com.regexp; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Tes ...

  7. 无法连接mysql,请检查mysql是否已启动及用户密码是否设置正确

    安装好后,登录后台提示 无法连接mysql,请检查mysql是否已启动及用户密码是否设置正确 检查mysql是否启动netstat -lnpt是否有3306端口? 一 有A 检查/www/wdlinu ...

  8. 最长公共子序列lcs 51nod1006

    推荐参考博客:动态规划基础篇之最长公共子序列问题 - CSDN博客  https://blog.csdn.net/lz161530245/article/details/76943991 个人觉得上面 ...

  9. 从零开始写bootloader(1)

          下面是具体的代码实现:  #define S3C2440_MPLL_200MHZ ((0x5C<<12)|(0x01<<4)|(0x02)) #define MEM ...

  10. 【C++ const_cast强制转换】