对 udp::socket::async_receive_from 的多个并行调用 - 未定义的行为?
我正在使用 boost::asio 创建服务器应用程序,特别是 UDP API。现在,我从池中启动多个线程,使用 async_receive_from 侦听单独的缓冲区。这在我主要开发的 Ubuntu 上非常有效。
然而,在针对 Windows 进行编译时,我的回调对于每个发送的缓冲区都会被多次调用,只有一个报告发现了某些内容,其他报告则报告发送了零字节。
我查看了 boost::asio 文档,我知道写/读自由函数必须串行调用 - 但我无法找到任何关于 async_receive_ 函数是否同样适用的信息。
我应该简单地调用 async_receive_ 一次吗?
I am using boost::asio to create a server application, specifically the UDP APIs. Right now I start multiple threads from a pool listening to separate buffers using async_receive_from. This works great on Ubuntu where I develop mostly.
However, when compiling for windows, my callback gets called many times for each sent buffer, with only one reporting that something was found, the others report that zero bytes were sent.
I have looked at the boost::asio docs, and I know that the write/read free functions must be called serially - but I have been unable to find any information as to whether the same holds for the async_receive_ functions.
Should I simply call async_receive_ once instead?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它适用于所有
async_xxx
函数,您绝不能在给定套接字上调用函数,直到前一个函数完成 - 否则您最终会得到垃圾(或未定义的行为)...编辑:我在 Linux 上工作,我很惊讶你看到它工作正常,因为我肯定没有。
编辑更多内容:按照文档中所示进行操作,即在 async_read_some 调用的处理程序中,例如,将下一次读取排队。
It holds for all the
async_xxx
functions, you must never call a function on a given socket till the previous one completes - or you'll end up with rubbish (or undefined behaviour)...EDIT: I work on linux, and I'm surprised that you see it working correctly, as I definitely don't.
EDIT SOME MORE: do it as shown in the docs, i.e. in the handler for the
async_read_some
call for example, queue the next read.