多线程同步回调
#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. Fragment的创建与通信

    由于这里涉及到接口回调的问题,所以先来看一看什么是接口回调: 这就好比老板和员工的微妙关系,老板需要员工去工作,员工挣钱了以后还要告诉老板自己挣了多少钱,然后由老板来处理这些钱. 首先创建一个接口: ...

  2. linux 操作系统级别监控 df 命令

    df命令可以查看当前系统磁盘空间的使用情况 命令:df -h du -sh * 查看目录文件暂用磁盘大小 如果磁盘空间不够,需清理磁盘 磁盘速度测试,如果磁盘性能不好,性能测试数据会不准确(读写速度) ...

  3. Chrome 查看产品原型图

    1.找到产品发的原型图 2.找到文件resources\chrome\axure-chrome-extension,修改文件的后缀为rar,然后解压 3.找到chrome的extensions,找到开 ...

  4. class命名归类

    常见class关键词: 布局类:header, footer, container, main, content, aside, page, section 包裹类:wrap, inner 区块类:r ...

  5. Game with string CodeForce#1104B 栈、串

    题目链接:Game with string 题目原文 Two people are playing a game with a string 

  6. C++中类型强制转换

    C++中强制类型转换有四种: 1.static_cast 格式:static_cast<Type>(Value); --用于基本类型间的转换,但不能用于基本类型指针间的转换: int i ...

  7. 阿里云ESC服务器centos6.9使用及注意事项

    阿里云ESC服务器,配置低,但是足够新手尝试操作练习. 使用之前,注意事项: 选择操作系统 设置实例快照 安装终端工具 一,选择操作系统. 可以在购买服务器的时候进行选择系统盘,也可以在购买之后在实例 ...

  8. KD-tree学习笔记(超全!)

    目录 K-D树 更新信息 建树 插入 查询 k远/近询问 重构 K-D 树优化建边 后记 因为之前找不到全的博客,唯一的一篇码风比较毒瘤... 所以我就来写了 K-D树 大概是高维二叉树吧 每次按一个 ...

  9. PHP 正则表达式的简单应用以 preg_match_all 抓取HTML元素为例 [转载]

    PHP 正则表达式的简单应用以 preg_match_all 抓取HTML元素为例 2011-12-02 17:09:39|  分类: PHP|举报|字号 订阅     下载LOFTER我的照片书   ...

  10. python使用代理ip

    python使用代理的方法有两种 1. #先创建代理ip对象 proxy_support = urllib.request.ProxyHandler({'https':'117.64.149.137: ...