boost之定时器和io_service
1.定时器的使用,sleep是等待线程,asio封装了操作系统的异步系统调用select,epoll.
io_servie 实现了一个任务队列,这里的任务就是void(void)的函数。Io_servie最常用的两个接口是post和run,post向任务队列中投递任务,run是执行队列中的任务,直到全部执行完毕,并且run可以被N个线程调用。Io_service是完全线程安全的队列。
#include <iostream>
#include <string>
#include <vector>
#include <boost/asio.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
using namespace std;
using namespace boost::asio;
using namespace boost; int main()
{
io_service ios;
deadline_timer t(ios,posix_time::seconds(2));
cout << t.expires_at() <<endl;
t.wait();
cout << "hello asio" <<endl;
return 0; }
2.定时器的作用是类似sendto,recvfrom等IO操作,相当于等待一段时间有数据到达,而io_service的作用类似select,服务于IO。
异步I/O调用
#include <iostream>
#include <string>
#include <vector>
#include <boost/asio.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
using namespace std;
using namespace boost::asio;
using namespace boost; void print(const system::error_code&)
{
cout << "hello asio" <<endl;
}
int main()
{
io_service ios;
deadline_timer t(ios,posix_time::seconds(2));
t.async_wait(print);
cout << "it show before t exired" <<endl;
ios.run();
return 0; }
boost之定时器和io_service的更多相关文章
- boost asio 学习(一)io_service的基础
原文 http://www.gamedev.net/blog/950/entry-2249317-a-guide-to-getting- started-with-boostasio/ 编译环境 b ...
- 使用boost线程定时器作为后台线程来切换主循环程序状态方法2
上一篇的方法主要使用的是:通过线程延时实 现的定时,并且只能定时一次,如果需要对此定时处理,就需要使用下面的定时器: #include "stdafx.h" #include &l ...
- 概念理解:boost::asio::定时器1
同步定时器 #include <cstdio> #include <iostream> #include <boost/asio.hpp> #include < ...
- boost timer 定时器 Operation cancel !
前面段时间处理一个定时器时,老是提示 操作取消. 硬是没搞明白为什么! 其实我遇到的这个情况很简单就是(boost::asio::deadline_timer timer)这个变量的生命同期结束了,对 ...
- <转>Boost库之asio io_service以及run、run_one、poll、poll_one区别
本文转自:http://blog.csdn.net/byxdaz/article/details/71088812 一.io_service的作用 io_servie 实现了一个任务队列,这里的任务就 ...
- Boost库之asio io_service以及run、run_one、poll、poll_one区别
一.io_service的作用 io_servie 实现了一个任务队列,这里的任务就是void(void)的函数.Io_servie最常用的两个接口是post和run,post向任务队列中投递任务,r ...
- 概念理解:boost::asio::定时器2
多线程同步回调#include <cstdio> #include <iostream> #include <boost/asio.hpp> #include &l ...
- 使用boost的asio,io_service无法初始化
今天用vs编一个用asio写的程序,发现在tcp::acceptor::open()失败,查了好久,发现是acceptor绑定的io_service没有正确的初始化,又查了半天,发现是需要加一个预编译 ...
- [Boost基础]并发编程——asio网络库——定时器deadline_timer
asio库基于操作系统提供的异步机制,采用前摄器设计模式(Proactor)实现了可移植的异步(或者同步)IO操作,而且并不要求使用多线程和锁定,有些的避免了多线程编程带来的诸多有害副作用(如条件竞争 ...
随机推荐
- rsync for windows 详细使用教程
rsync for windows 详细使用教程内容简介:rsync在windows与windows服务器之间的同步设置 1.准备两台机器: server-----192.168.0.201 clie ...
- php的pear包管理
1.安装: $ sudo wget http://pear.php.net/go-pear.phar $ sudo php go-pear.har 2.查看pear下安装的包: $ pear l ...
- vc2010, fatal error LNK1123: failure during conversion to COFF: file invalid or corrupt解决办法
是因为安其它软件的时候更新了.net framework,导致vc2010出了问题. 解决办法是在系统里搜索cvtres.exe,会搜到很多,把其中 Microsoft Visual Studio 1 ...
- Xcode, does not contain bitcode. You must rebuild it with bitcode enabled (Xcode setting ENABLE_BITCODE) 解决办法
在Build Settings中找到Enable Bitcode项,设置为如下: 参考:http://blog.csdn.net/soindy/article/details/48519363
- 【转】jQuery.extend 函数详解
原文地址:http://www.cnblogs.com/RascallySnake/archive/2010/05/07/1729563.html JQuery的extend扩展方法: Jq ...
- 基于Away3D实现全景的相机控制器。
最近研究打算做个全景的Demo,发现Away3D本身的天空盒跟全景属于两种完全不同东西.最后只能基于HoverController来扩展(原因是HoverController能提供的距离控制,类似拉近 ...
- java - day13 - UnionPay
银行功能实现,主要为抽象类及接口的运用 package com.javatest.bankimple; /* UnionPay */ //银联接口 public interface UnionPay ...
- js使用正则表达式验证身份证格式
function checkIdentity(identity){ var reg = /^[1-9]{1}[0-9]{14}$|^[1-9]{1}[0-9]{16}([0-9]|[xX])$/; i ...
- 几种垃圾回收GC概述
垃圾回收机制 引用计数回收器(Reference Counting Collector) 原理是在每个对象内部维护一个整数值,叫做这个对象的引用计数,当对象被引用时引用计数加一,当对象不被引用时引用计 ...
- HibernateTools实现pojo类 数据库schma mapping映射的相互转换 二
接着上一篇博客:HibernateTools实现pojo类 数据库schma mapping映射的相互转换 思路二:由数据库表,生成Mapping映射文件和POJO类. 尽管能够实现,但个人觉着先设计 ...