Boost::asio 和嵌套/递归 service.run()/run_one()

发布于 2024-12-13 17:27:18 字数 456 浏览 0 评论 0原文

io_service.run_one() 的嵌套调用是否可能不是 boost::asio 的有效使用。

例如,我有一个处理程序,它在套接字上收到某些内容后执行。 io_service 正在另一个线程中通过 io_service.run_one() 运行。因此,我认为 io_service 将在为接收处理程序提供服务后停止。

然后在接收处理程序中,我再次执行 io_service.run_one() 以便现在通过套接字发送回一些内容。这意味着存在 io_service.run_one() 方法的嵌套/递归调用。

这种行为实际上是行不通的。我的意思是有时 run_one() 方法中有一个永远不会返回的块。我也找不到任何有关此特定案例的文档处理。

那么,是否允许递归/嵌套 io_service 执行,或者这不是一个好的行为?

PS io_service.reset() 在每次 run_one() 执行之前使用。

Is it possible that nested calls of io_service.run_one() is not a valid use of boost::asio.

For example I have a handler which is executed after something has been received on a socket. The io_service is running in another thread with io_service.run_one(). Hence in my opinion the io_service will stop after serving the receive handler.

Then in the receive handler I am executing io_service.run_one() again in order to send now something back over the socket. This means, that there is a nested/recursive call of io_service.run_one() method.

This behavior doesn't really work. I mean sometimes there is a block in the run_one() method which never returns. I couldn't also find any documentation handling about this particular case.

So, is it allowed to have recursive/nested io_service execution at all or is this not a well behavior?

P.S. io_service.reset() is used in front of every run_one() execution.

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

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

发布评论

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

评论(1

葵雨 2024-12-20 17:27:18

据我所知,这既不是预期用途,也没有在 asio 中记录,所以看起来一个安全的假设是:不,它无效。

现在,它暂时可以在某些或所有平台上运行。但这并不一定是一个好主意。

另外,如果您不想在处理程序内部阻塞,调用 run_one(),您可能需要考虑 poll_one()

不管怎样,这样做听起来确实很可疑。假设您假设正在发送一条特定的消息,以便您可以立即执行一些操作?我只能想到非常人为的示例,其中可以安全地假设两个消息在 asio 消息队列中是背靠背的。

As far as I know, this is neither intended use nor documented in asio, so it would seem like a safe assumption is: no, it's not valid.

Now, it may work, on some or all platforms, for now. That doesn't necessarily make it a good idea though.

Also, if you intend to not block inside your handlers, calling run_one(), you might want to consider poll_one().

Either way, it definitely sounds suspicious to do this. Presumably you're assuming that a specific message is being dispatched, so that you can do some work immediately following it? I can only think of very contrived examples where it would be a safe assumption that two messages are back-to-back in the asio message queue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文