Boost::Asio :io_service.run() 与 poll() 或者如何在主循环中集成 boost::asio

发布于 2024-10-12 03:29:48 字数 424 浏览 4 评论 0原文

我目前第一次尝试使用 boost::asio 进行一些简单的 tcp 网络,我已经遇到了一些我不太确定如何处理的事情。据我了解 io_service.run() 方法基本上是一个循环,它运行直到没有什么可做的,这意味着它将运行直到我释放我的小服务器对象。由于我已经设置了某种主循环,为了简单起见,我宁愿从那里手动更新网络循环,而且我认为 io_service.poll() 会做我想要的事情,有点像这样

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

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

Smile简单爱 2024-10-19 03:29:48

使用 io_service::poll 代替 io_service::run 是完全可以接受的。差异在 文档

也可以使用 poll() 函数
派遣准备好的处理程序,但是
无阻塞。

请注意,如果有任何work 留在队列中

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

而 io_service::poll 不会表现出这种行为,它只是调用就绪的处理程序。另请注意,您需要调用 io_service::reset 在任何后续调用 io_service:runio_service::poll 时。

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.

笔落惊风雨 2024-10-19 03:29:48

一个缺点是你会形成一个繁忙的循环。

while(true) {
    myIoService.poll()
}

将使用 100% CPU。 myIoService.run() 将使用 0% cpu。

myIoService.run_one() 可能会做你想做的事情,但如果没有什么可做的,它会阻塞。

A drawback is that you'll make a busy loop.

while(true) {
    myIoService.poll()
}

will use 100% cpu. myIoService.run() will use 0% cpu.

myIoService.run_one() might do what you want but it will block if there is nothing for it to do.

浮云落日 2024-10-19 03:29:48

像这样的循环允许您轮询,不忙等待,并根据需要重置。 (我正在使用更新的 io_context 来替换 io_service。)

while (!exitCondition) {
    if (ioContext.stopped()) {
        ioContext.restart();
    }
    if (!ioContext.poll()) {
        if (stuffToDo) {
            doYourStuff();
        } else {
            std::this_thread::sleep_for(std::chrono::milliseconds(3));
        }
    }
}

A loop like this lets you poll, doesn't busy-wait, and resets as needed. (I'm using the more recent io_context that replaced io_service.)

while (!exitCondition) {
    if (ioContext.stopped()) {
        ioContext.restart();
    }
    if (!ioContext.poll()) {
        if (stuffToDo) {
            doYourStuff();
        } else {
            std::this_thread::sleep_for(std::chrono::milliseconds(3));
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文