Chinaunix首页 | 论坛 | 博客
  • 博客访问: 563417
  • 博文数量: 104
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1559
  • 用 户 组: 普通用户
  • 注册时间: 2014-08-21 00:58
个人简介

锻炼精神,首先要锻炼肉体

文章分类

全部博文(104)

文章存档

2018年(1)

2016年(1)

2015年(101)

2014年(1)

我的朋友

分类: C/C++

2015-06-10 16:05:41

这篇博文中的示例代码简单演示了如何结合 Boost 中的 bind 与 Boost.Asio 中的异步方法来实现简单的异步处理方法.

代码简单的模拟了编译器编译源文件的过程,其异步操作的进度是按照时间顺序进行的,程序的大致流程如下所示:
首先,compiler 运行,执行异步事件链中的首个异步操作----> 读入带编译的源文件
读入源文件之后-----> 等待 2 s ------> 执行异步事件链中的第二个事件 ------> 编译读入的源文件
编译源文件之后 -----> 等待 2 s -----> 执行异步事件链中的第三个事件 -------> 执行链接、绑定操作
连接绑定操作之后-----> 等待 2 s ----> 执行异步事件链中的第四个事件 ------> 执行运行编译生成的可执行程序

到达截止时间,程序结束运行,输出结束语并结束程序.


示例代码如下

点击(此处)折叠或打开

  1. #include <iostream>
  2. #include <boost/asio.hpp>
  3. #include <boost/bind.hpp>
  4. #include <boost/date_time/posix_time/posix_time.hpp>

  5. class compiler
  6. {
  7.  public :
  8.     compiler ( boost::asio::io_service &io ):
  9.         timer_(io,boost::posix_time::seconds(1)) ,counter_(0)
  10.     {
  11.      timer_.async_wait( boost::bind(&compiler::read_in_source_file ,this) ) ;
  12.      // timer_ 是 boost::deadline_timer 的实例对象,在这里调用 async_wait 该成员方法
  13.     // 目的是为了在截止时间到来的时候调用 其内部绑定的 compiler 类中的成员方法 read_in_source_file
  14.     //-------
  15.     // 这里的 bind 方法的使用因为其绑定的是类-成员方法而区别于普通方法的绑定
  16.     // bind 的第一个参数是 类名称::被绑定的类成员方法名称
  17.     // bind 的第二个参数是 类对象实例,因为绑定操作发生在 类内部,所以传入的是 this ,this 用于在类内部表示类实例对象
  18.     // 类似于 java 编程中的 this.name = name ; 这种构造方法中的赋值操作
  19.     }

  20.     ~compiler ()
  21.     {
  22.      std::cout << "finish compiling " << std::endl ;
  23.     }
  24.     
  25.     void read_in_source_file ()
  26.     {
  27.      std::cout << " read in source file " << std::endl ;
  28.     
  29.      timer_.expires_at( timer_.expires_at() + boost::posix_time::seconds(2) ) ;
  30.       //   在这里将截止时间延迟 2 秒,2 秒过后将会调用绑定的 compile_source_file 函数

  31.      timer_.async_wait (boost::bind(&compiler::compile_source_file , this ) ) ;
  32.     }

  33.     void compile_source_file ()
  34.     {
  35.      std::cout << "compiling source file " << std::endl ;
  36.     
  37.      timer_.expires_at(timer_.expires_at() + boost::posix_time::seconds(2) ) ;
  38.     
  39.      timer_.async_wait ( boost::bind(&compiler::link_bind_step , this )) ;
  40.     }
  41.     
  42.     void link_bind_step ()
  43.     {
  44.      std::cout << "linking and binding target files" << std::endl ;
  45.     
  46.      timer_.expires_at(timer_.expires_at() + boost::posix_time::seconds(2)) ;
  47.     
  48.      timer_.async_wait ( boost::bind( &compiler::run_program_step , this )) ;
  49.     }
  50.     
  51.     void run_program_step ()
  52.     {
  53.      timer_.expires_at(timer_.expires_at() + boost::posix_time::seconds(2)) ;
  54.       // 上面的这个用来延迟截止时间的方法调不调用都可以,因为这个 run_program_step 方法已经是异步操作链中的最后的一个操作了
  55.       // 2s 过后将会退出程序
  56.      std::cout << "executing executable binary file " << std::endl ;
  57.     
  58.      std::cout << " time end " << std::endl ;
  59.     }
  60.  private :
  61.     int counter_ ;
  62.     boost::asio::deadline_timer timer_ ;
  63. } ;

  64. int main ()
  65. {
  66.     boost::asio::io_service io ;
  67.     compiler c(io ) ;
  68.     
  69.     io.run () ;
  70.     return 0 ;
  71. }

end
阅读(1547) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~