东方耀AI技术分享

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
热搜: 活动 交友 discuz
查看: 3024|回复: 0
打印 上一主题 下一主题

[C/C++] boost库的网络编程asio(每隔固定时间干活)

[复制链接]

1365

主题

1856

帖子

1万

积分

管理员

Rank: 10Rank: 10Rank: 10

积分
14441
QQ
跳转到指定楼层
楼主
发表于 2021-10-14 14:31:36 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式




    std::cout << "boost库的网络编程asio(每隔固定时间干活)" << endl;
    // 用到的技术点:asio io_service async_wait boost::bind expires_at





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


  5. using namespace std;

  6. void print(const boost::system::error_code&, boost::asio::deadline_timer *t, int *count){
  7.     if(*count < 5){
  8.         std::cout << "次数:" << *count << std::endl;
  9.         ++(*count);
  10.         t->expires_at(t->expires_at() + boost::posix_time::seconds(1));
  11.         // 有了lambda函数后不推荐用bind了
  12.         //"boost" 没有成员 "bind  要引入:#include <boost/bind.hpp>
  13.         t->async_wait(boost::bind(print, boost::asio::placeholders::error, t, count));
  14.     }

  15. }


  16. void callback(const boost::system::error_code&){
  17.     std::cout << "1秒后干的事情!" << endl;
  18. }


  19. void async_call(){
  20.     int count=0;
  21.     boost::asio::io_service io;
  22.     // 异步调用模式下:可以注册多个事件 循环调用 多次重复
  23.     // 事件的注册 1
  24.     boost::asio::deadline_timer t(io, boost::posix_time::seconds(1));
  25.     // t.async_wait(callback);
  26.     t.async_wait(boost::bind(print, boost::asio::placeholders::error, &t, &count));
  27.    
  28.     std::cout << "直接到这,不阻塞" << endl;

  29.     io.run();  // 阻塞是在run里的,run()相当于一个while循环 结束条件:1、io.stop() 2、没有事件了

  30.     std::cout << "最终的结果:" << count << std::endl;
  31.    
  32. }

  33. int main(){

  34.     //target_link_libraries(main -lboost_system)
  35.     std::cout << "boost库的网络编程asio(每隔固定时间干活)" << endl;
  36.     // 用到的技术点:asio io_service async_wait boost::bind expires_at

  37.     async_call();

  38.     return 0;
  39. }
复制代码



不用boost::bind,而用lambda函数在编译的时候会内联展开 效率比function要高




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


  4. using namespace std;

  5. void print(const boost::system::error_code&, boost::asio::deadline_timer& t, int& count){
  6.     if(count < 5){
  7.         std::cout << "次数:" << count << std::endl;
  8.         ++count;
  9.         t.expires_at(t.expires_at() + boost::posix_time::seconds(1));
  10.         // 有了lambda函数后不推荐用bind了
  11.         t.async_wait([&t, &count](const auto& error){
  12.             print(error, t, count);
  13.         });
  14.     }

  15. }


  16. void callback(const boost::system::error_code&){
  17.     std::cout << "1秒后干的事情!" << endl;
  18. }


  19. void async_call(){
  20.     int count=0;
  21.     boost::asio::io_service io;
  22.     // 异步调用模式下:可以注册多个事件 循环调用 多次重复
  23.     // 事件的注册 1
  24.     boost::asio::deadline_timer t(io, boost::posix_time::seconds(1));
  25.     // t.async_wait(callback);
  26.     // t.async_wait(boost::bind(print, boost::asio::placeholders::error, &t, &count));
  27.     // 不用boost::bind,而用lambda函数在编译的时候会内联展开 效率比function要高
  28.     t.async_wait([&t, &count](const auto& error){
  29.         print(error, t, count);
  30.     });
  31.    
  32.     std::cout << "直接到这,不阻塞" << endl;

  33.     io.run();  // 阻塞是在run里的,run()相当于一个while循环 结束条件:1、io.stop() 2、没有事件了

  34.     std::cout << "最终的结果:" << count << std::endl;
  35.    
  36. }

  37. int main(){

  38.     //target_link_libraries(main -lboost_system)
  39.     std::cout << "boost库的网络编程asio(每隔固定时间干活)不用boost::bind" << endl;
  40.     // 用到的技术点:asio io_service async_wait boost::bind expires_at

  41.     async_call();

  42.     return 0;
  43. }
复制代码







让天下人人学会人工智能!人工智能的前景一片大好!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|人工智能工程师的摇篮 ( 湘ICP备2020019608号-1 )

GMT+8, 2024-5-28 13:20 , Processed in 0.175684 second(s), 18 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表