多线程同步回调
#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. nested exception is java.lang.NoClassDefFoundError: javax/xml/soap/SOAPElement

    JavaSE 8 includes package java.xml.soap.JavaSE 9 moved package javax.xml.soap to the module java.xml ...

  2. C++基础之string类

    string也是属于顺序容器,但是string类很重要且经常使用,因此在这里单独记录. string的操作总结 string(const char *s,int n);  //用c字符串s初始化,s应 ...

  3. 搭建vagrant开发环境

    最近正好用着Vagrant搭建开发环境,写一篇文章记录一下. Vagrant目前是国内互联网公司应用最多的内部开发环境工具. Mac. Windows搭建是一样的,我是在Mac下搭建的环境. vagr ...

  4. [phyton]文件的简单读写练习

    f.open() 用于打开一个文件. f=open("record.txt","w",encoding="utf-8")#打开文件,设置文件 ...

  5. Google AppCrawler初探

    AppCrawler是什么 你可以把它想成类似monkey一样的工具,调起你的应用程序并执行各种动作(点击,输入,滑动等)来通过这种方式 来查看各种情况下应用程序的状态 官方文档链接:AppCrawl ...

  6. javascript中数组常用的方法和属性

    前言 在javascript中,数组是一种非常重要的数据类型,我们时常会和它打交道,最近在开发项目中频繁的使用到数组,但是自己对数组的众多方法已经是非常模糊了,为了方便自己以后能够更好的使用数组中的属 ...

  7. springboot 集成swagger2

    使用Swagger 可以动态生成Api接口文档,在项目开发过程中可以帮助前端开发同事减少和后端同事的沟通成本,而是直接参照生成的API接口文档进行开发,提高了开发效率.这里以springboot(版本 ...

  8. linux下mysql数据库操作命令

    1:启动服务 service mysqld start (5.0版本是mysqld) service mysql start (5.5.7版本是mysql) 2:停止服务 service mysqld ...

  9. 无广告版本Flash player 一枚,需要的拿走~

    无广告版本flashplayer,国内flashplayer已经被植入了广告,安装后不断的弹出广告使用体验很差,最开始还以为是电脑被恶意植入捆绑插件, 检查半天发现竟然是浏览器的flash playe ...

  10. pywintypes.com_error: (-2147352567, '发生意外 解决方案

    在使用win32com处理Excel的时候,遇到pywintypes.com_error: (-2147352567 错误. 百度.Google各种方法不得其解,和以下网页类似——http://blo ...