多线程同步回调
#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. Springboot+Shiro+Mybatis+mysql

    一 .shiro框架 Shiro是Apache 的一个强大且易用的Java安全框架,执行身份验证.授权.密码学和会话管理.Shiro 主要分为两个部分就是认证和授权两部分 1.Subject代表了当前 ...

  2. vue2.0搭建vue手脚架(vue-cli)

    1.安装node.js 从node官网下载并安装node,安装步骤很简单,只要一路“next”就可以了.安装完成后,打开命令行工具输入命令node -v,如下图,如果出现对应版本号,就说明安装成功了. ...

  3. 树莓派3安装ros

    树莓派3上面安装ros总结参考wiki和诸多博客安装ros仍然遇到了很多问题,重装了好几遍才成功,为了自己和其他人以后再安装ros时,不在重蹈覆辙.1.准备和说明树莓派3,安装系统Raspbian j ...

  4. maven仓库 - nexus配置

    搭建环境: 腾讯云服务器 CentOS 6.8.jdk7.sonatype nexus.maven.Xshell 5 版本信息: jdk : jdk-7u80-linux-x64.tar.gz nex ...

  5. 一起看期待已久的.NET Core 3.0新的单文件部署特性,记在昨日VS2019更新后

    VS2019又又又迎来一次新的更新,这次的重点在.NET Core, 妥妥的更新好,默默地反选2.2,一切都在意料之中. 这次我们来看VS2019的新特性单文件部署: https://www.talk ...

  6. 【linux】【tomcat】tomcat8.5安装

    系统环境:Centos7 1.下载tomcat8.5 wget http://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-8/v8.5.45/b ...

  7. vscode Springboot 启动debug报错:Build failed, do you want to continue?

    一,前言 vscode我感觉是一个特别好用的开发工具,我根据文章https://www.cnblogs.com/WangBoBlog/p/9464281.html去搭建一个简单的springboot工 ...

  8. Android蓝牙低功耗(BLE)模块设计

    在阅读这篇文章之前你应该对GATT和Android蓝牙框架有一定的了解.这里不会向你解释Service.Characteristics等蓝牙知识.这里只是我写下我对Android Ble的再次封装来适 ...

  9. EJB生成代码后遇到transient错误

    启动服务的时候遇到这样的错误: 解决方案: 1.找到对应的模块的SesBean文件 2.去掉transient 3.重启服务即可

  10. 初级Python

    [toc] 一.数据类型 1.1基本数据类型 1.1.1数字类型 1.整数类型 十进制:1010,-219 二进制:以0b或0B开头:0b010,-0B101 八进制,以0o或0O开头:0o123,- ...