Boost :: Asio:io_service.run()vs poll()或者如何在mainloop中集成boost :: asio

时间:2022-09-06 20:59:00

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()?

2 个解决方案

#1


38  

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将阻止

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::reset on any subsequent invocation to io_service:run or io_service::poll.

而io_service :: poll没有表现出这种行为,它只是调用现成的处理程序。另请注意,您需要在对io_service:run或io_service :: poll的任何后续调用中调用io_service :: reset。

#2


2  

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()可能会执行您想要的操作,但如果没有任何操作可以阻止它。

#1


38  

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将阻止

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::reset on any subsequent invocation to io_service:run or io_service::poll.

而io_service :: poll没有表现出这种行为,它只是调用现成的处理程序。另请注意,您需要在对io_service:run或io_service :: poll的任何后续调用中调用io_service :: reset。

#2


2  

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()可能会执行您想要的操作,但如果没有任何操作可以阻止它。