boost库学习随记六:使用同步定时器、异步定时器、bind、成员函数回调处理、多线程的同步处理示例等

一、使用同步定时器

这个示例程序通过展示如何在一个定时器执行一个阻塞等待。

[cpp] view plaincopy

  1. //makefile
  2. #----------------------------------------------------------
  3. #makefile helloworld测试用例
  4. #
  5. #
  6. #
  7. #
  8. #-----------------------------------------------------------
  9. ggg=g++
  10. exe=asiotimer
  11. #所有的.o文件写在这里
  12. obj = asiotimer.o
  13. #所要关联的cpp文件写在这里
  14. cpp = asiotimer.cpp
  15. #加入库文件
  16. libso = -lboost_thread -lboost_system
  17. $(exe):$(obj)
  18. @echo "链接开始................"
  19. $(ggg) $(libso) -o $(exe) $(obj)
  20. hw.o : $(cpp)
  21. @echo "编译开始................"
  22. $(ggg) -std=c++11 -c $(cpp)
  23. .PHONY : clean cleanall
  24. cleanall:
  25. @echo "开始make all..........."
  26. -rm -rf $(exe) $(obj)
  27. clean:
  28. @echo "开始清理................"
  29. -rm -rf $(obj)

2、asiotimer.h头文件

[cpp] view plaincopy

  1. //asiotimer.h
  2. #ifndef __ASIOTIMER__H__
  3. #define __ASIOTIMER__H__
  4. #include <iostream>
  5. #include <boost/asio.hpp>
  6. //#define BOOST_DATE_TIME_SOURCE
  7. #include "boost/date_time/posix_time/posix_time.hpp"
  8. #endif

3、asiotimer.cpp文件

[cpp] view plaincopy

  1. //asiotimer.cpp
  2. #include "asiotimer.h"
  3. int main()
  4. {
  5. boost::asio::io_service io;
  6. boost::asio::deadline_timer t(io,boost::posix_time::seconds(5));
  7. t.wait();
  8. std::cout<<"hello,world\n";
  9. return 0;
  10. }

二、使用异步定时器示例

本示例程序演示了如何使用Asio的异步回调功能由示例一修改程序 ,开启计时器执行一个异步等待。

1、makefile文件

makefile 与示例一基本相同,只需要修改
exe=asiotest2

#所有的.o文件写在这里
obj = asiotest2.o

#所要关联的cpp文件写在这里
cpp = asiotest2.cpp

2、asiotest2.h

[cpp] view plaincopy

  1. #ifndef __ASIOTEST2__H__
  2. #define __ASIOTEST2__H__
  3. #include <iostream>
  4. #include <boost/asio.hpp>
  5. #include <boost/date_time/posix_time/posix_time.hpp>
  6. void print(const boost::system::error_code& );
  7. #endif

3、asiotest2.cpp

[cpp] view plaincopy

  1. #include "asiotest2.h"
  2. using namespace std;
  3. using namespace boost;
  4. void print(const boost::system::error_code& )
  5. {
  6. std::cout<<"hello,world!\n";
  7. }
  8. int main()
  9. {
  10. boost::asio::io_service io;
  11. boost::asio::deadline_timer t(io,boost::posix_time::seconds(5));
  12. t.async_wait(&print);
  13. io.run();
  14. return 0;
  15. }

三、绑定参数到处理程序

在本示例中,我们将在示例二修改程序,使定时器每秒被激活一次。这将显示如何传递额外的参数给你的处理函数。

1、makefile 文件同示例二makefile修改方法

2、头文件

[cpp] view plaincopy

  1. #ifndef __ASIOTEST3__H__
  2. #define __ASIOTEST3__H__
  3. #include <iostream>
  4. #include <boost/asio.hpp>
  5. #include <boost/bind.hpp>
  6. #include <boost/date_time/posix_time/posix_time.hpp>
  7. #endif

3、CPP文件

[cpp] view plaincopy

  1. #include "asiotest3.h"
  2. void print(const boost::system::error_code&,
  3. boost::asio::deadline_timer* t,int* count)
  4. {
  5. if(*count<5)
  6. {
  7. std::cout<<*count<<"\n";
  8. ++(*count);
  9. t->expires_at(t->expires_at() + boost::posix_time::seconds(1));
  10. t->async_wait(boost::bind(print,
  11. boost::asio::placeholders::error,t,count));
  12. }
  13. }
  14. int main()
  15. {
  16. boost::asio::io_service io;
  17. int count=0;
  18. boost::asio::deadline_timer t(io,boost::posix_time::seconds(1));
  19. t.async_wait(boost::bind(print,boost::asio::placeholders::error,
  20. &t,&count));
  21. io.run();
  22. std::cout<<"Final count is" <<count<<"\n";
  23. return 0;
  24. }

四、使用成员函数做为处理程序示例

在本示例中,我们将看到如何使用一个类的成员函数作为回调处理程序。

1、makefile 同上面示例

2、头文件

[cpp] view plaincopy

  1. #ifndef __ASIOTEST4__H__
  2. #define __ASIOTEST4__H__
  3. #include <iostream>
  4. #include <boost/asio.hpp>
  5. #include <boost/bind.hpp>
  6. #include <boost/date_time/posix_time/posix_time.hpp>
  7. class printer
  8. {
  9. public:
  10. printer(boost::asio::io_service& io)
  11. :timer_(io,boost::posix_time::seconds(1)),
  12. count_(0)
  13. {
  14. timer_.async_wait(boost::bind(&printer::print,this));
  15. }
  16. ~printer()
  17. {
  18. std::cout<<"Final count is "<<count_<<"\n";
  19. }
  20. void print()
  21. {
  22. if(count_<5)
  23. {
  24. std::cout<<count_<<std::endl;
  25. ++count_;
  26. timer_.expires_at(timer_.expires_at()+boost::posix_time::seconds(1));
  27. timer_.async_wait(boost::bind(&printer::print,this));
  28. }
  29. }
  30. private:
  31. boost::asio::deadline_timer timer_;
  32. int count_;
  33. };

3、cpp文件

[cpp] view plaincopy

  1. #include "asiotest4.h"
  2. int main()
  3. {
  4. boost::asio::io_service io;
  5. printer p(io);
  6. io.run();
  7. return 0;
  8. }

五、多线程的同步处理示例

本示例演示boost::asio::strand 在多线程程序中同步回调处理程

1、makefile同上

2、头文件

[cpp] view plaincopy

  1. #ifndef __ASIOTEST5__H__
  2. #define __ASIOTEST5__H__
  3. #include <iostream>
  4. #include <boost/asio.hpp>
  5. #include <boost/thread/thread.hpp>
  6. #include <boost/bind.hpp>
  7. #include <boost/date_time/posix_time/posix_time.hpp>
  8. class printer
  9. {
  10. public:
  11. printer(boost::asio::io_service& io):strand_(io),
  12. timer1_(io,boost::posix_time::seconds(1)),
  13. timer2_(io,boost::posix_time::seconds(1)),count_(0)
  14. {
  15. timer1_.async_wait(strand_.wrap(boost::bind(&printer::print1,this)));
  16. timer2_.async_wait(strand_.wrap(boost::bind(&printer::print2,this)));
  17. }
  18. ~printer()
  19. {
  20. std::cout<<"Final count is " <<count_<<std::endl;
  21. }
  22. void print1()
  23. {
  24. if(count_ < 10)
  25. {
  26. std::cout<<"Timer 1: "<<count_<<std::endl;
  27. ++count_;
  28. timer1_.expires_at(timer1_.expires_at() + boost::posix_time::seconds(1));
  29. timer1_.async_wait(strand_.wrap(boost::bind(&printer::print1,this)));
  30. }
  31. }
  32. void print2()
  33. {
  34. if(count_ < 10)
  35. {
  36. std::cout<<"Timer 2: " <<count_<<std::endl;
  37. ++count_;
  38. timer2_.expires_at(timer2_.expires_at() + boost::posix_time::seconds(1));
  39. timer2_.async_wait(strand_.wrap(boost::bind(&printer::print2,this)));
  40. }
  41. }
  42. private:
  43. boost::asio::strand strand_;
  44. boost::asio::deadline_timer timer1_;
  45. boost::asio::deadline_timer timer2_;
  46. int count_;
  47. };
  48. #endif

3、CPP文件

[cpp] view plaincopy

  1. #include "asiotest5.h"
  2. int main()
  3. {
  4. boost::asio::io_service io;
  5. printer p(io);
  6. boost::thread t(boost::bind(&boost::asio::io_service::run,&io));
  7. io.run();
  8. t.join();
  9. return 0;
  10. }
      from:

http://blog.csdn.net/leitianjun/article/details/25740633

时间: 2024-08-02 23:01:39

boost库学习随记六:使用同步定时器、异步定时器、bind、成员函数回调处理、多线程的同步处理示例等的相关文章

c++ boost库学习三:实用工具

noncopyable 大家都知道定义一个空类的时候,它实际包含了构造函数,拷贝构造函数,赋值操作符和析构函数等. 这样就很容易产生一个问题,就是当用户调用A a(“^_^") 或者A c="^_^" 时会发生一些意想不到的行为,所以很多时候我们需要禁用这样的用法. 一种方法就是把拷贝构造函数和赋值操作符显式的定义为private,但是这样需要很多代码. 于是boost库为大家提供了一个简单的方法:只需要将类继承于noncopyable就可以了. #include "

c++ boost库学习一:时间和日期

timer类 #include <boost\timer.hpp> #include "iostream" using namespace std; int _tmain(int argc, _TCHAR* argv[]) { boost::timer t; cout<<"max time span: "<<t.elapsed_max()/3600<<"h"<<endl; //596.5

C++ Primer 学习笔记_28_操作符重载与转换(3)--成员函数的重载、覆盖与隐藏、类型转换运算符、*运算符重载、-&gt;运算符重载

C++ Primer 学习笔记_28_操作符重载与转换(3)--成员函数的重载.覆盖与隐藏.类型转换运算符.*运算符重载.->运算符重载 一.成员函数的重载.覆盖与隐藏 对于类层次的同名成员函数来说,有三种关系:重载.覆盖和隐藏,理清3种关系,有助于写出高质量的代码. 1.成员函数的重载 重载的概念相对简单,只有在同一类定义中的同名成员函数才存在重载关系,主要特点时函数的参数类型和数目有所不同:但不能出现函数参数的个数和类型均相同,仅仅依靠返回值类型不同来区分的函数,这和普通函数的重载是完全一致

boost库学习之regex

一.背景 项目中许多地方需要对字符串进行匹配,比如根据指定的过滤字符串来过滤文件名.刚开始是排斥使用boost库的,第一,我不熟悉boost库:第二,如果引入第三方库,就会增加库的依赖,这样的后果是,要么打包程序时,打包动态库,要么直接使用静态库编译,会使增大程序的大小. 刚开始是尝试自己写模糊匹配算法,很简单,就只支持_和%,这两个通配符,然后发现Linux下有一个fnmatch的函数,就是进行模糊匹配的,它支持shell通配符. 但是到最后发现,当需要区别很相似的字符串时,模糊匹配就不行了,

boost库学习之开篇

本系列文章使用boost_1.58.0版本. 一.欢迎使用boost C++库 boost致力于提供一个免费的.便携的源代码级的库. 我们重视那些与C++标准一起工作良好的库.boost库将要成为一个应用广泛的库,成为应用程序可以依赖的平台.boost证书估计商业和非商业机构使用它. 我们的目标是建立已存在的练习而且提供对于库具体实现的引用以至于boost库适合于最后的标准.十个boost库已经包含在C++标准委员会的TR1而且将要被包含在即将到来的C++标准版本中.更多的boost库将目标放在

Boost库学习之旅入门篇

学习及使用Boost库已经有一段时间了,Boost为我的日常开发中带来了极大的方便,也使得我越来越依赖于boost库了.但boost功能太多,每次使用还是得翻看以前的 资料,所以为了以后可以更方便的使用,在此对常用的功能作一个总结,也希望以此与大家共勉. boost库下载,Svn地址: http://svn.boost.org/svn/boost/trunk 编译源码 编译boost库自带脚本解释工具: 使用vs自带命令行工具Visual Studio 命令提示(2010),运行bat脚本boo

boost库学习之 date_time库

 date_time库是一个全面灵活的日期时间库,提供时间相关的各种所需功能,也是一个比较复杂的库.它支持从1400-01-01到9999-12-31之间的日期计算.使用时包含#include <boost/date_time/gregorian/gregorian.hpp>头文件, 引用boost::gregorian;命名空间. 日期 date是date_time库中的核心类.以天为单位表示时间点. date d1; //无效日期 date d2(2015, 1, 4); date d

boost库学习之 pool库

简单来说内存池预先分配了一块大的内存空间,然后在其中使用某种算法高效快速的自定制内存分配. pool库包含四个组成部分,最简单的pool,分配类实例的object pool,单件内存池singleton_tool和用于标准库的pool_alloc. 为了使用pool组件,需要包含<boost/pool/pool.hpp>头文件. 操作函数: pool的构造函数接受一个size_type类型的整数request_size,指示每次pool分配内存块的大小 成员函数get_request_size

boost库学习之 scoped_ptr scoped_array

boost.smart_ptr库提供了六种智能指针:scoped_ptr.scoped_array.shared_ptr.shared_array.week_ptr和intrusive_ptr. 说到智能指针,我们会想到c++98标准中的自动指针auto_ptr. auto_ptr获取指针所有权后,离开作用域时自动释放该指针指向的堆内存.也可以转移指针的所有权. auto_ptr<A> ap_a1(new A); auto_ptr<A> ap_a2(ap_a1); //发生所有权转