同步定时器
#include <cstdio>
#include <iostream>
#include <boost/asio.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
int main()
{
boost::asio::io_service io;
boost::asio::deadline_timer t(io, boost::posix_time::seconds());
t.wait();//同步定时器,5后被调用
std::cout << "Hello, world!\n";
return ;
} 链接库 -lpthread -lboost_system
异步定时器
#include <cstdio>
#include <iostream>
#include <boost/asio.hpp>
#include <boost/thread.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
using namespace std; /****************************************************************
函 数:Print
参 数:void
返回值:const boost::system::error_code &ec -- 执行失败原因
说 明:回调函数
****************************************************************/
void Print(const boost::system::error_code &ec)
{
cout << "Hello World!" << endl;
cout << boost::this_thread::get_id() << endl;//id一致, 在同一个线程中执行
} int main()
{
cout << boost::this_thread::get_id() << endl;//id一致, 在同一个线程中执行
boost::asio::io_service io;
boost::asio::deadline_timer t(io, boost::posix_time::seconds());
t.async_wait(Print);//异步定时器,立马返回
cout << "to run" << endl;
io.run(); //异步IO调度
cout << "exit" << endl; return EXIT_SUCCESS;
}
回调函数绑定参数 boost::bind()
/****************************************************************
函 数:Print
参 数:const boost::system::error_code &ec -- 执行失败原因
参 数:boost::asio::deadline_timer* pt -- 定时器
参 数:int * pcount -- 执行次数
返回值:void
说 明:回调函数
****************************************************************/
void Print(const boost::system::error_code &ec, boost::asio::deadline_timer* pt, int* pcount)
{
if (*pcount < )
{
cout << "count = " << *pcount << endl;
(*pcount)++;
//推迟定时器的终止时间,确保回调函数不会在所需时间内到期
pt->expires_at(pt->expires_at() + boost::posix_time::seconds());
//重新绑定任务
pt->async_wait(boost::bind(Print, boost::asio::placeholders::error, pt, pcount));
}
} int main()
{
boost::asio::io_service io;
boost::asio::deadline_timer t(io, boost::posix_time::seconds());
int count = ;
t.async_wait(boost::bind(Print, boost::asio::placeholders::error, &t, &count));
cout << "to run" << endl;
io.run();
cout << "Final count is " << count << "\n";//
cout << "exit" << endl;
return ;
}

概念理解:boost::asio::定时器1的更多相关文章

  1. 概念理解:boost::asio::定时器2

    多线程同步回调#include <cstdio> #include <iostream> #include <boost/asio.hpp> #include &l ...

  2. 概念理解:boost::asio::io_service

    IO模型 io_service对象是asio框架中的调度器,所有异步io事件都是通过它来分发处理的(io对象的构造函数中都需要传入一个io_service对象). asio::io_service i ...

  3. boost::asio::deadline_timer(理解)

    并发与并行: 并发和并行从宏观上来讲都是同时处理多路请求的概念.但并发和并行又有区别,并行是指两个或者多个事件在同一时刻发生:而并发是指两个或多个事件在同一时间间隔内发生. 1.Timer.1 - 使 ...

  4. boost asio 学习(六) 定时器

    http://www.gamedev.net/blog/950/entry-2249317-a-guide-to-getting- started-with-boostasio?pg=7 6 定时器 ...

  5. boost::asio学习(定时器)

    #include <boost/asio.hpp> #include <iostream> void handle1(const boost::system::error_co ...

  6. boost::asio::io_service::定时器任务队列

    使用io_service和定时器写的一个同步和异步方式的任务队列 #pragma once #include <string> #include <iostream> #inc ...

  7. boost.asio新框架的设计概念总结

    1.66版本,boost.asio库重新设计了框架,目前最新版为1.71.读了几天代码后,对框架中相关概念总结.因为是泛型编程的库,所以分析的概念层的设计. 可通过boost官方文档,strand的1 ...

  8. boost.asio源码剖析(四) ---- asio中的泛型概念(concepts)

    * Protocol(通信协议) Protocol,是asio在网络编程方面最重要的一个concept.在第一章中的levelX类图中可以看到,所有提供网络相关功能的服务和I/O对象都需要Protoc ...

  9. Boost asio基本概念

    asio库基于操作系统提供的异步机制,采用前摄器模式(Proactor)实现可移植的异步(或同步)IO操作,不需要使用多线程和锁,有效避免多线程编程带来的诸多有害副作用(如竞争,死锁). asio封装 ...

随机推荐

  1. 链表实现比较高效的删除倒数第k项

    最近写链表不太顺,无限的段错误.今天中午写的链表删除倒数第k项,用的带尾节点的双向链表,感觉已经把效率提到最高了,还是超时,改了很多方法都不行,最 终决定看博客,发现原来是审题错了,阳历给的是以-1结 ...

  2. TestNG(三) 基本注解BeforeMethod和AfterMethod

    package com.course.testng; import org.testng.annotations.*; public class BasicAnnotation { @Test //最 ...

  3. centos7下mysql 用户管理和权限设置

    1.进入mysql命令行,输入root及密码[root@localhost ~]# mysql -u root -pEnter password: Welcome to the MySQL monit ...

  4. SpringBoot的注解注入功能移植到.Net平台(开源)

    *:first-child { margin-top: 0 !important; } .markdown-body>*:last-child { margin-bottom: 0 !impor ...

  5. 实操:Could not autowire No beans of 'FastDFS Client' type found 的解决方法

    前言: 今天接手了同事之前做的一个小项目,里面涉及到了 FastDFS 的使用.但是当我在本地运行项目的时候,却报了 Could not autowire No beans of 'FastDFS C ...

  6. SPSS学习笔记参数检验—单样本t检验

    单样本t检验 目的:利用来自总体的样本数据,推断该总体的均值是否与指定的检验值存在差异. 适用条件:样本来自的总体应服从或者近似服从正态分布. 注:当样本量n比较大时:由中心极限定理得知,即使原数据不 ...

  7. (java实现)单链表

    什么是单链表 在了解单链表之前,你知道什么是链表吗?如果你不知道什么是链表,可以看看我的这篇博客<链表-LinkList> 单链表是链表的其中一种基本结构.一个最简单的结点结构如图所示,它 ...

  8. 1.python环境配置 - python基础入门

    工欲善其事必先利其器,python学习首先要做得就是配置python环境.配置环境只需要下载Pycharm 和 Anaconda两个安装包即可,请跟上我得步伐,一步一步操作. 重要的事情说三遍: 先安 ...

  9. dependencies 与 dependencyManagement 区别

    dependencies 即使在子项目中不写该依赖项,那么子项目仍然会从父项目中继承该依赖项(全部继承)dependencyManagement 里只是声明依赖,并不实现引入,因此子项目需要显示的声明 ...

  10. C语言I博客作业02

    这个作业属于那个课程  C语言程序设计I 这个作业要求在哪 https://edu.cnblogs.com/campus/zswxy/CST2019-3/homework/8656 我在这个课程的目标 ...