鱼C论坛

 找回密码
 立即注册
查看: 3642|回复: 1

[技术交流] 《C++Boost库asio》第四篇 io_server在多线程下如何使用

[复制链接]
发表于 2017-7-9 12:31:03 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
io_server在多线程下如何使用:
因为有可能最终逻辑是,ioserver只处理网路部分的信息,外带time,把接收玩家信息和发送放到一

个额外线程里,主循环跑的是主逻辑。也有可能在翻过来让iosever在主线程,逻辑在分线程,2钟方

法都可以
int main() {
boost::asio::io_service io;
printer p (io);//一定要等所有事件定义好了才能开线程
std::thread t([&io]{io.run();});//逻辑有可能在分线程跑
io.run();//逻辑也有可能在主线程做处理
t.join();
return;
}

多线程如何同步回调:
boost::asio::io_service::strand strand_;
timer1_.async_wait(strand_.wrap([this](const auto&){this->print1();}));
timer2_.async_wait(strand_.wrap([this](const auto&){this->print2();}));
strand_.wrap这个可以保证在多线程中执行的顺序一定是按照你定义的顺序来的,如果1没有执行,2

就不会先执行

注意:
1.一定要等所有事件定义好了才能开线程
2.io.run一旦结束,就进入到stop状态,其他线程下的io.run也跟着改变了,如果要再次启动,一定

要调用
if (io.stopped()) {
io.reset();
}

例子:
#include <iostream>
#include <boost/asio.hpp>
#include <thread>
#include <boost/date_time/posix_time/posix_time.hpp>

class printer {
public:
  printer(boost:;asio::io_server &io) : strand_(io), timer1_

(io,boost::posix_timer::seconds(1)), timer2_(io,boost::posix_time::seconds(1)),count_(0)

{
   timer1_.async_wait(strand_.wrap([this](const auto&){this->print1();}));
   timer2_.async_wait(strand_.wrap([this](const auto&){this->print2();}));
  }

  ~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([this](const auto&){this->print1();}));
 }

  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([this](const auto&){this->print2;}));
 }
  }

private:
  boost::asio::io_service::strand strand_;
  boost::asio::deadline_timer timer1_;
  boost:;asio:;deadline_timer timer2_;
  int count_;
};

int main() {
boost:;asio::io_service io;
printer p(io);
std::thread t([&io]{io.run();});
io.run();
t.join();

return 0;
}

评分

参与人数 1鱼币 +3 收起 理由
小甲鱼 + 3 支持楼主!

查看全部评分

本帖被以下淘专辑推荐:

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2017-7-11 06:58:26 | 显示全部楼层
io_server在多线程下如何使用

同步回调

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

  5. class printer
  6. {
  7.     public:
  8.     printer(boost:; asio::io_server &io) :
  9.         strand_(io),
  10.         timer1_(io,boost::posix_timer::seconds(1)),
  11.         timer2_(io,boost::posix_time::seconds(1)),
  12.         count_(0)
  13.     {
  14.         timer1_.async_wait(strand_.wrap([this](const auto&)
  15.         {
  16.             this->print1();
  17.         }));
  18.         timer2_.async_wait(strand_.wrap([this](const auto&)
  19.         {
  20.             this->print2();
  21.         }));
  22.     }

  23.     ~printer()
  24.     {
  25.         std::cout << "Final count is " << count_  << std::endl;
  26.     }

  27.     void print1()
  28.     {
  29.         if (count_< 10)
  30.     {
  31.         std::cout << "Timer 1: " << count_ << std:: endl;
  32.         ++count_;

  33.         timer1_.expires_at(timer1_.expires_at() + boost::posix_time::seconds(1));
  34.             timer1_.async_wait(strand_.wrap([this](const auto&)
  35.             {
  36.                 this->print1();
  37.             }));
  38.              
  39.         }

  40.         void print2()
  41.         {
  42.             if (count_ < 10)
  43.         {
  44.             std::cout << "Timer 2: " << count_ << std:: endl;
  45.             ++count_;

  46.             timer2_.expires_at(timer2_.expires_at() + boost::posix_time::seconds(1));
  47.                 timer2_.async_wait(strand_.wrap([this](const auto&)
  48.                 {
  49.                     this->print2;
  50.                 }));
  51.                  
  52.             }
  53.         }

  54.         private:
  55.         boost::asio::io_service::strand strand_;
  56.         boost::asio::deadline_timer timer1_;
  57.         boost:; asio:; deadline_timer timer2_;
  58.         int count_;
  59.     };

  60.     int main()
  61.     {
  62.         boost::asio::io_service io;

  63.         //一定要等所有事件定义好了才能开线程
  64.         printer p (io);

  65.         std::thread t([&io] {io.run();}); //逻辑有可能在分线程跑
  66.         io.run();//逻辑也有可能在主线程做处理

  67.         t.join();// 断开;

  68.         return;
  69.     }
复制代码


注意:
1.一定要等所有事件定义好了才能开线程
2.io.run一旦结束,就进入到stop状态,其他线程下的io.run也跟着改变了,如果要再次启动,一定要调用
if (io.stopped()) {
    io.reset();
}
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-21 01:34

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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