概念理解:boost::asio::定时器2
多线程同步回调
#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的更多相关文章
- 概念理解:boost::asio::定时器1
同步定时器 #include <cstdio> #include <iostream> #include <boost/asio.hpp> #include < ...
- 概念理解:boost::asio::io_service
IO模型 io_service对象是asio框架中的调度器,所有异步io事件都是通过它来分发处理的(io对象的构造函数中都需要传入一个io_service对象). asio::io_service i ...
- boost::asio::deadline_timer(理解)
并发与并行: 并发和并行从宏观上来讲都是同时处理多路请求的概念.但并发和并行又有区别,并行是指两个或者多个事件在同一时刻发生:而并发是指两个或多个事件在同一时间间隔内发生. 1.Timer.1 - 使 ...
- boost asio 学习(六) 定时器
http://www.gamedev.net/blog/950/entry-2249317-a-guide-to-getting- started-with-boostasio?pg=7 6 定时器 ...
- boost::asio学习(定时器)
#include <boost/asio.hpp> #include <iostream> void handle1(const boost::system::error_co ...
- boost::asio::io_service::定时器任务队列
使用io_service和定时器写的一个同步和异步方式的任务队列 #pragma once #include <string> #include <iostream> #inc ...
- boost.asio新框架的设计概念总结
1.66版本,boost.asio库重新设计了框架,目前最新版为1.71.读了几天代码后,对框架中相关概念总结.因为是泛型编程的库,所以分析的概念层的设计. 可通过boost官方文档,strand的1 ...
- boost.asio源码剖析(四) ---- asio中的泛型概念(concepts)
* Protocol(通信协议) Protocol,是asio在网络编程方面最重要的一个concept.在第一章中的levelX类图中可以看到,所有提供网络相关功能的服务和I/O对象都需要Protoc ...
- Boost asio基本概念
asio库基于操作系统提供的异步机制,采用前摄器模式(Proactor)实现可移植的异步(或同步)IO操作,不需要使用多线程和锁,有效避免多线程编程带来的诸多有害副作用(如竞争,死锁). asio封装 ...
随机推荐
- ansible-playbook流程控制-loops循环使用
1. ansible-playbook流程控制-loops循环使用 有时你想要多次重复任务.在计算机编程中,这称为循环.common ansible循环包括使用文件模块更改多个文件和/或目录的所 ...
- js-数据交互--AJAX
一:介绍 今天跟下大家简单的介绍一下,在前端开发中,前后端数据交互的一种手段,我们都知道,在前端往后端传送数据的话,利用get,post方法即可向后端发送数据,后端将数据接受,链接到数据库,进行数据库 ...
- 32 (OC)* keyChain的本质
1:它是一个sqlite数据库,其保存的所有数据都是加密过的. 2:Keychain是加密规则(key)的集合.每个规则必须含有以下三个要素:认证算法.认证密钥(加密字符串).规则的时间. 3:key ...
- 21 (OC) 数据持久化
概论 所谓的持久化,就是将数据保存到硬盘中,使得在应用程序或机器重启后可以继续访问之前保存的数据.在iOS开发中,有很多数据持久化的方案,接下来我将尝试着介绍一下5种方案: plist文件(属性列表) ...
- JAVA用递归来判断回文串
用递归来判断回文串 思路: ①首先如果是空串,一个字符,则判断是回文串 ②若不满足①,则我们需要对字符串进行递归判断,我们会想到要用第一个字符和最后一个字符进行比较,若相同则,第二个和倒数第二个进行比 ...
- Matplotlib散点图、条形图、直方图-02
对比常用统计图 折线图: 特点:能够显示数据的变化趋势,反映事物的变化情况.(变化) 直方图: 特点:绘制连续性的数据,展示一组或者多组数据的分布情况(统计) 条形图: 特点:绘制离散的数据,能够一眼 ...
- Linux-----centos6.2---安装Linux的流程
1.安装命令 # yum install mysql-server mysql 2.查看是否安装正确 # chkconfig --list mysqld 3.启动 (1).启动服务: # serv ...
- Autofac的AOP面向切面编程研究
*:first-child { margin-top: 0 !important; } .markdown-body>*:last-child { margin-bottom: 0 !impor ...
- 数据分析--pandas的基本使用
一.pandas概述 1.pandas是一个强大的Python数据分析的工具包,是基于NumPy构建的. 2.pandas的主要功能 具备对其功能的数据结构DataFrame.Series 集成时间序 ...
- bootstrap具体知识点(2)
3.以移动设备为优先 <meta name=”viewport” content=”width=device-width,initial-scale=1,user-scalable=no”> ...