C++ Boost::Asio : io_service.run() vs poll() 或者我如何在 mainloop 中集成 boost::asio
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4705411/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Boost::Asio : io_service.run() vs poll() or how do I integrate boost::asio in mainloop
提问by moka
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:
我目前正第一次尝试将 boost::asio 用于一些简单的 tcp 网络,但我已经遇到了一些我不确定如何处理的问题。据我了解 io_service.run() 方法基本上是一个循环,它会一直运行直到没有其他事情要做,这意味着它会一直运行直到我释放我的小服务器对象。由于我已经设置了某种主循环,为了简单起见,我宁愿从那里手动更新网络循环,而且我认为 io_service.poll() 会做我想做的,有点像这样:
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?
这似乎有效,但我仍然想知道这种方法是否有缺点,因为这似乎不是处理 boost::asios io 服务的常用方法。这是一种有效的方法还是我应该在非阻塞额外线程中使用 io_service.run() ?
回答by Sam Miller
Using io_service::poll
instead of io_service::run
is perfectly acceptable. The difference is explained in the documentation
使用io_service::poll
代替io_service::run
是完全可以接受的。文档中解释了差异
The poll() function may also be used to dispatch ready handlers, but without blocking.
poll() 函数也可用于调度就绪处理程序,但不会阻塞。
Note that io_service::run
will block if there's any work
left in the queue
请注意,io_service::run
如果work
队列中还有剩余,则会阻塞
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.
工作类用于在工作开始和完成时通知 io_service。这确保了 io_service 对象的 run() 函数在工作正在进行时不会退出,并且在没有未完成的工作时会退出。
whereas io_service::poll
does not exhibit this behavior, it just invokes ready handlers. Also note that you will need to invoke io_service::reseton any subsequent invocation to io_service:run
or io_service::poll
.
而io_service::poll
不会表现出这种行为,它只是调用就绪处理程序。另请注意,您将需要在对或 的任何后续调用时调用io_service::reset。io_service:run
io_service::poll
回答by Kurt
A drawback is that you'll make a busy loop.
一个缺点是你会做一个繁忙的循环。
while(true) {
myIoService.poll()
}
will use 100% cpu. myIoService.run()
will use 0% cpu.
将使用 100% 的 CPU。myIoService.run()
将使用 0% cpu。
myIoService.run_one()
might do what you want but it will block if there is nothing for it to do.
myIoService.run_one()
可能会做你想做的事,但如果它无事可做,它就会阻塞。