Boost :: Asio:io_service.run()vs poll()或如何在mainloop中集成boost :: asio [英] Boost::Asio : io_service.run() vs poll() or how do I integrate boost::asio in mainloop

查看:743
本文介绍了Boost :: Asio:io_service.run()vs poll()或如何在mainloop中集成boost :: asio的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正试图使用​​boost :: asio的一些简单的tcp网络的第一次,我已经遇到了一些我不知道如何处理的东西。据我所知io_service.run()方法基本上是一个循环,直到没有更多的事情要做,这意味着它将运行,直到我释放我的小服务器对象。由于我已经有一些mainloop设置,我宁愿更新网络环路手动从那里只是为了简单,我认为io_service.poll()将做我想要的,这样: / p>

  void myApplication :: update()
{
myIoService.poll
//做其他东西
}

这似乎工作,我仍然想知道是否有一个缺点从这种方法,因为这似乎不是常见的方式来处理boost :: asios io服务。这是一个有效的方法,还是应该在非阻塞的额外线程中使用io_service.run()?

解决方案

c $ c> io_service :: poll 而不是 io_service :: run 是完全可以接受的。区别在文档中进行了说明


也可以使用poll()函数
来分派就绪的处理程序,但
不会阻塞。


请注意, io_service :: run 会阻止,如果有任何 工作


工作类用于在工作开始和
完成时通知
io_service。这确保
io_service对象的run()函数
在工作正在进行时不会退出,
,当没有
未完成的工作时,它会退出。


io_service :: poll 没有展示这种行为,它只是调用了处理程序。另请注意,您需要调用 io_service :: reset $

I am currently trying to use boost::asio for some simple tcp networking for the first time, and I allready came across something I am not really sure how to deal with. As far as I understand io_service.run() method is basically a loop which runs until there is nothing more left to do, which means it will run until I release my little server object. Since I allready got some sort of mainloop set up, I would rather like to update the networking loop manually from there just for the sake of simplicity, and I think io_service.poll() would do what I want, sort of like this:

void myApplication::update()
{
     myIoService.poll();
     //do other stuff
}

This seems to work, but I am still wondering if there is a drawback from this method since that does not seem to be the common way to deal with boost::asios io services. Is this a valid approach or should I rather use io_service.run() in a non blocking extra thread?

解决方案

Using io_service::poll instead of io_service::run is perfectly acceptable. The difference is explained in the documentation

The poll() function may also be used to dispatch ready handlers, but without blocking.

Note that io_service::run will block if there's any work left in the queue

The work class is used to inform the io_service when work starts and finishes. This ensures that the io_service object's run() function will not exit while work is underway, and that it does exit when there is no unfinished work remaining.

whereas io_service::poll does not exhibit this behavior, it just invokes ready handlers. Also note that you will need to invoke io_service::reset on any subsequent invocation to io_service:run or io_service::poll.

这篇关于Boost :: Asio:io_service.run()vs poll()或如何在mainloop中集成boost :: asio的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆