多线程同步回调
#include <cstdio>
#include <iostream>
#include <boost/asio.hpp>
#include <boost/thread.hpp>
#include <boost/asio/strand.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
using namespace boost;
using namespace std;
class CPrinter
{
public:
CPrinter(boost::asio::io_service &io)
:m_strand(io)
, m_timer1(io, boost::posix_time::seconds())
, m_timer2(io, boost::posix_time::seconds())
, m_count()
{
m_timer1.async_wait(m_strand.wrap(boost::bind(&CPrinter::Print1, this)));
m_timer2.async_wait(m_strand.wrap(boost::bind(&CPrinter::Print2, this)));
}
~CPrinter()
{
cout << "m_count = " << m_count << endl;
}
void Print1()
{
if (m_count < )
{
cout << "Timer1 count = " << m_count << endl;
cout << boost::this_thread::get_id() << endl;
m_count++;
m_timer1.expires_at(m_timer1.expires_at() + boost::posix_time::seconds());
m_timer1.async_wait(m_strand.wrap(boost::bind(&CPrinter::Print1, this)));
}
}
void Print2()
{
if (m_count < )
{
cout << "Timer2 count = " << m_count << endl;
cout << boost::this_thread::get_id() << endl;
m_count++;
m_timer2.expires_at(m_timer2.expires_at() + boost::posix_time::seconds());
m_timer2.async_wait(m_strand.wrap(boost::bind(&CPrinter::Print2, this)));
}
}
private:
//strand提供串行执行, 能够保证线程安全, 同时被post或dispatch的方法, 不会被并发
boost::asio::strand m_strand;
boost::asio::deadline_timer m_timer1;
boost::asio::deadline_timer m_timer2;
int m_count;
}; int main()
{
cout << boost::this_thread::get_id() << endl;
boost::asio::io_service io;
CPrinter cp(io);
cout << "to run" << endl;
boost::thread td(boost::bind(&boost::asio::io_service::run, &io));
io.run();
cout << "exit" << endl; return EXIT_SUCCESS;
}

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

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

    同步定时器 #include <cstdio> #include <iostream> #include <boost/asio.hpp> #include < ...

  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. 降低 80% 的读写响应延迟!我们测评了 etcd 3.4 新特性(内含读写发展史)

    作者 | 陈洁(墨封)  阿里云开发工程师 导读:etcd 作为 K8s 集群中的存储组件,读写性能方面会受到很多压力,而 etcd 3.4 中的新特性将有效缓解压力,本文将从 etcd 数据读写机制 ...

  2. CSS——字体

    1.字体样式font-family.颜色color <!DOCTYPE html> <html> <head> <meta charset="UTF ...

  3. python django+bootstrap4+mysql智慧交通系统构建

    之前,我做了一个实训的项目,但是一直没有展示如何做的,现在就让我讲解一下如何用django+bootstrap4+mysql实现这个智慧交通系统.这里用到的是网页的bootstrap4框架和mysql ...

  4. 新手学习FFmpeg - 调用API完成两个视频的任意合并

    本次尝试在视频A中的任意位置插入视频B. 在上一篇中,我们通过调整PTS可以实现视频的加减速.这只是对同一个视频的调转,本次我们尝试对多个视频进行合并处理. Concat如何运行 ffmpeg提供了一 ...

  5. activity的隐式和显式启动

    显式Intent(Explicit intent):通过指定Intent组件名称来实现的,它一般用在知道目标组件名称的前提下,一般是在相同的应用程序内部实现的. 隐式Intent(Implicit i ...

  6. Selenium+python上传本地文件或者图片

    基于input标签的,有属性type = file: 首先定位到点击上传的元素(input)然后直接使用send_keys()将文件在本地的路径传进去 代码如下(例子来源于本页面上传图片和文件): f ...

  7. PyCharm中创建项目时,在所创建的python虚拟环境下的pip失效

    在这篇博文里,我简单地叙述了我在使用PyCharm创建一个flask项目时遇到的问题,以及我解决这个问题的过程.其中比较值得注意的点有:①PyCharm创建新项目时的解释器配置②Python虚拟环境的 ...

  8. if [ $# -ne 1 ] 作用

    在shell脚本中经常会使用if [ $# -ne 1 ];then...这类脚本 ];then 这段命令是用于判断参数的个数是否为1,不是则进行then的逻辑处理,其中$#表示参数个数,-ne是不等 ...

  9. Ubuntu使用vi命令时,不能正常编辑文件,使用方向键时老是出现很多字母解决方案

    原因是系统只装了vi,没有装vim.因为vi是不能直接按退格键删除字符的.所以重新装下vim指令即可: # sudo apt-get install vim 重新使用vi命令进行文件编辑.

  10. springboot 使用freemarker自定义标签

    1.pom依赖引入 <dependencies> <dependency> <groupId>org.springframework.boot</groupI ...