boost库学习随记六:使用同步定时器、异步定时器、bind、成员函数回调处理、多线程的同步处理示例等
一、使用同步定时器
这个示例程序通过展示如何在一个定时器执行一个阻塞等待。
- //makefile
- #----------------------------------------------------------
- #makefile helloworld测试用例
- #
- #
- #
- #
- #-----------------------------------------------------------
- ggg=g++
- exe=asiotimer
- #所有的.o文件写在这里
- obj = asiotimer.o
- #所要关联的cpp文件写在这里
- cpp = asiotimer.cpp
- #加入库文件
- libso = -lboost_thread -lboost_system
- $(exe):$(obj)
- @echo "链接开始................"
- $(ggg) $(libso) -o $(exe) $(obj)
- hw.o : $(cpp)
- @echo "编译开始................"
- $(ggg) -std=c++11 -c $(cpp)
- .PHONY : clean cleanall
- cleanall:
- @echo "开始make all..........."
- -rm -rf $(exe) $(obj)
- clean:
- @echo "开始清理................"
- -rm -rf $(obj)
2、asiotimer.h头文件
- //asiotimer.h
- #ifndef __ASIOTIMER__H__
- #define __ASIOTIMER__H__
- #include <iostream>
- #include <boost/asio.hpp>
- //#define BOOST_DATE_TIME_SOURCE
- #include "boost/date_time/posix_time/posix_time.hpp"
- #endif
3、asiotimer.cpp文件
- //asiotimer.cpp
- #include "asiotimer.h"
- int main()
- {
- boost::asio::io_service io;
- boost::asio::deadline_timer t(io,boost::posix_time::seconds(5));
- t.wait();
- std::cout<<"hello,world\n";
- return 0;
- }
二、使用异步定时器示例
本示例程序演示了如何使用Asio的异步回调功能由示例一修改程序 ,开启计时器执行一个异步等待。
1、makefile文件
makefile 与示例一基本相同,只需要修改
exe=asiotest2
#所有的.o文件写在这里
obj = asiotest2.o
#所要关联的cpp文件写在这里
cpp = asiotest2.cpp
2、asiotest2.h
- #ifndef __ASIOTEST2__H__
- #define __ASIOTEST2__H__
- #include <iostream>
- #include <boost/asio.hpp>
- #include <boost/date_time/posix_time/posix_time.hpp>
- void print(const boost::system::error_code& );
- #endif
3、asiotest2.cpp
- #include "asiotest2.h"
- using namespace std;
- using namespace boost;
- void print(const boost::system::error_code& )
- {
- std::cout<<"hello,world!\n";
- }
- int main()
- {
- boost::asio::io_service io;
- boost::asio::deadline_timer t(io,boost::posix_time::seconds(5));
- t.async_wait(&print);
- io.run();
- return 0;
- }
三、绑定参数到处理程序
在本示例中,我们将在示例二修改程序,使定时器每秒被激活一次。这将显示如何传递额外的参数给你的处理函数。
1、makefile 文件同示例二makefile修改方法
2、头文件
- #ifndef __ASIOTEST3__H__
- #define __ASIOTEST3__H__
- #include <iostream>
- #include <boost/asio.hpp>
- #include <boost/bind.hpp>
- #include <boost/date_time/posix_time/posix_time.hpp>
- #endif
3、CPP文件
- #include "asiotest3.h"
- void print(const boost::system::error_code&,
- boost::asio::deadline_timer* t,int* count)
- {
- if(*count<5)
- {
- std::cout<<*count<<"\n";
- ++(*count);
- t->expires_at(t->expires_at() + boost::posix_time::seconds(1));
- t->async_wait(boost::bind(print,
- boost::asio::placeholders::error,t,count));
- }
- }
- int main()
- {
- boost::asio::io_service io;
- int count=0;
- boost::asio::deadline_timer t(io,boost::posix_time::seconds(1));
- t.async_wait(boost::bind(print,boost::asio::placeholders::error,
- &t,&count));
- io.run();
- std::cout<<"Final count is" <<count<<"\n";
- return 0;
- }
四、使用成员函数做为处理程序示例
在本示例中,我们将看到如何使用一个类的成员函数作为回调处理程序。
1、makefile 同上面示例
2、头文件
- #ifndef __ASIOTEST4__H__
- #define __ASIOTEST4__H__
- #include <iostream>
- #include <boost/asio.hpp>
- #include <boost/bind.hpp>
- #include <boost/date_time/posix_time/posix_time.hpp>
- class printer
- {
- public:
- printer(boost::asio::io_service& io)
- :timer_(io,boost::posix_time::seconds(1)),
- count_(0)
- {
- timer_.async_wait(boost::bind(&printer::print,this));
- }
- ~printer()
- {
- std::cout<<"Final count is "<<count_<<"\n";
- }
- void print()
- {
- if(count_<5)
- {
- std::cout<<count_<<std::endl;
- ++count_;
- timer_.expires_at(timer_.expires_at()+boost::posix_time::seconds(1));
- timer_.async_wait(boost::bind(&printer::print,this));
- }
- }
- private:
- boost::asio::deadline_timer timer_;
- int count_;
- };
3、cpp文件
- #include "asiotest4.h"
- int main()
- {
- boost::asio::io_service io;
- printer p(io);
- io.run();
- return 0;
- }
五、多线程的同步处理示例
本示例演示boost::asio::strand 在多线程程序中同步回调处理程
1、makefile同上
2、头文件
- #ifndef __ASIOTEST5__H__
- #define __ASIOTEST5__H__
- #include <iostream>
- #include <boost/asio.hpp>
- #include <boost/thread/thread.hpp>
- #include <boost/bind.hpp>
- #include <boost/date_time/posix_time/posix_time.hpp>
- class printer
- {
- public:
- printer(boost::asio::io_service& io):strand_(io),
- timer1_(io,boost::posix_time::seconds(1)),
- timer2_(io,boost::posix_time::seconds(1)),count_(0)
- {
- timer1_.async_wait(strand_.wrap(boost::bind(&printer::print1,this)));
- timer2_.async_wait(strand_.wrap(boost::bind(&printer::print2,this)));
- }
- ~printer()
- {
- std::cout<<"Final count is " <<count_<<std::endl;
- }
- void print1()
- {
- if(count_ < 10)
- {
- std::cout<<"Timer 1: "<<count_<<std::endl;
- ++count_;
- timer1_.expires_at(timer1_.expires_at() + boost::posix_time::seconds(1));
- timer1_.async_wait(strand_.wrap(boost::bind(&printer::print1,this)));
- }
- }
- void print2()
- {
- if(count_ < 10)
- {
- std::cout<<"Timer 2: " <<count_<<std::endl;
- ++count_;
- timer2_.expires_at(timer2_.expires_at() + boost::posix_time::seconds(1));
- timer2_.async_wait(strand_.wrap(boost::bind(&printer::print2,this)));
- }
- }
- private:
- boost::asio::strand strand_;
- boost::asio::deadline_timer timer1_;
- boost::asio::deadline_timer timer2_;
- int count_;
- };
- #endif
3、CPP文件
- #include "asiotest5.h"
- int main()
- {
- boost::asio::io_service io;
- printer p(io);
- boost::thread t(boost::bind(&boost::asio::io_service::run,&io));
- io.run();
- t.join();
- return 0;
- }
- from:
boost库学习随记六:使用同步定时器、异步定时器、bind、成员函数回调处理、多线程的同步处理示例等的更多相关文章
- QML学习笔记(六)- 简单计时器和定时器
做一个简单的qml计时器和定时器,左键触发计时,右键触发定时 GitHub:八至 作者:狐狸家的鱼 本文链接:QML学习笔记(六)- 简单计时器和定时器 左键点击按钮,触发计时器,中键可以暂停计时,同 ...
- boost库学习之开篇
本系列文章使用boost_1.58.0版本. 一.欢迎使用boost C++库 boost致力于提供一个免费的.便携的源代码级的库. 我们重视那些与C++标准一起工作良好的库.boost库将要成为一个 ...
- boost库学习之regex
一.背景 项目中许多地方需要对字符串进行匹配,比如根据指定的过滤字符串来过滤文件名.刚开始是排斥使用boost库的,第一,我不熟悉boost库:第二,如果引入第三方库,就会增加库的依赖,这样的后果是, ...
- Boost库学习之旅入门篇
学习及使用Boost库已经有一段时间了,Boost为我的日常开发中带来了极大的方便,也使得我越来越依赖于boost库了.但boost功能太多,每次使用还是得翻看以前的 资料,所以为了以后可以更方便的使 ...
- c++ boost库学习三:实用工具
noncopyable 大家都知道定义一个空类的时候,它实际包含了构造函数,拷贝构造函数,赋值操作符和析构函数等. 这样就很容易产生一个问题,就是当用户调用A a(“^_^") 或者A c= ...
- boost库在工作(39)网络UDP异步服务端之九
前面创建的UDP服务器和客户端,都是同步的方式,也就是说当接收数据时,不能参与别的事情执行的.如果在一个只有界面线程的程序里,又不想创建多线程,导致复杂程度的增加,在这种情况之下,我们还有一个方案可以 ...
- c++ boost库学习一:时间和日期
timer类 #include <boost\timer.hpp> #include "iostream" using namespace std; int _tmai ...
- c++ boost库学习二:内存管理->智能指针
写过C++的人都知道申请和释放内存组合new/delete,但同时很多人也会在写程序的时候忘记释放内存导致内存泄漏.如下所示: int _tmain(int argc, _TCHAR* argv[]) ...
- Python_sklearn机器学习库学习笔记(六) dimensionality-reduction-with-pca
# 用PCA降维 #计算协方差矩阵 import numpy as np X=[[2,0,-1.4], [2.2,0.2,-1.5], [2.4,0.1,-1], [1.9,0,-1.2]] np.c ...
随机推荐
- 初探ListView
ListView可能是Android开发中最常用的一个控件,但要用的纯熟还需要不断的锻炼. 建立简单的ListView 1.在布局文件(.xml)中添加<ListView>标签 2.在Ma ...
- MATLAB - 为什么imshow(g,[])可以正常显示,而imshow(g)却显示空白图像?
Q:为什么imshow(g,[])可以正常显示,而imshow(g)却显示空白图像? A:数据类型如果是double,imshow的处理范围是0-1数据类型如果是uint8,imshow的处理范围是0 ...
- jQuery相关面试题
1 你在公司是怎么用jquery的? 答:在项目中是怎么用的是看看你有没有项目经验(根据自己的实际情况来回答) 你用过的选择器啊,复选框啊,表单啊,ajax啊,事件等 配置Jquery环境 下载jqu ...
- imx:MfgTool
MfgTool使用方法: MfgTool很妖,写进去的img的名字一定要符合配置文件里面的命名标准. 具体要参见: MFG_TOOL\Profiles\Linux\OS Firmware ...
- BZOJ 1189: [HNOI2007]紧急疏散evacuate( BFS + 二分答案 + 匈牙利 )
我们可以BFS出每个出口到每个人的最短距离, 然后二分答案, 假设当前答案为m, 把一个出口拆成m个表示m个时间, 点u到出口v的距离为d, 那么u->v的[d, m]所有点连边, 然后跑匈牙利 ...
- BZOJ 1708: [Usaco2007 Oct]Money奶牛的硬币( dp )
背包dp.. -------------------------------------------------------------------------------- #include< ...
- hrtimer 高精定时器使用例子
在kernel,有个线程每个十秒往上层系统上报battery及USB的状态信息. 加入直接sleep 10 秒的话,插入USB的时候不能及时更新状态,这个时候就可以使用 hrtimer + wait ...
- (Problem 42)Coded triangle numbers
The nth term of the sequence of triangle numbers is given by, tn = ½n(n+1); so the first ten triangl ...
- IOS 学习笔记(5) 控件 文本视图(UITextView)的使用方法
相对于UILabell所支持的较短文本内容,UITextView对于长文本的支持更好.UITextView能够以滚动的方式全部浏览到长文本,并且就像UILabel那样,从ISO6,他也提供了对NSAt ...
- [转] IOS中AppDelegate中的生命周期事件的调用条件
IOS中AppDelegate中的生命周期事件的调用条件 //当应用程序将要进入非活动状态执行,在此期间,应用程序不接受消息或事件,比如来电 - (void)applicationWillResign ...