boost::asio 套接字 async_* 链

发布于 2024-11-27 05:28:36 字数 169 浏览 3 评论 0 原文

如何通过strand对socket执行async_*操作?我看过 Timer.5(Boost/Asio 示例),但它们仅显示如何调用用户的处理程序。当我在多线程应用程序中async_write到套接字时,数据可能会被写入损坏。 strand 保证这些处理程序不会同时执行。

How to perform async_* operations on socket through the strand? I've looked at Timer.5 (Boost/Asio examples), but they only show how to invoke user's handler. When I async_write to the socket in multithreaded application data may be written corrupted. And a strand guarantees that none of those handlers will execute concurrently.

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

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

发布评论

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

评论(2

浪漫之都 2024-12-04 05:28:37

来自 Boost.Asio 文档:

io_service::strand 类提供了发布和分派的能力
处理程序,并保证这些处理程序都不会执行
同时。

一个很好的例子 Boost.Asio 示例中的 strand 用法。

strand 保证您的处理程序执行将同步。这意味着如果您的 io_service 从多个线程执行,则 strand 非常有用。它与您如何安排任务(处理程序)无关。

strand 无法帮助您同时执行多个套接字读或写操作,因为内部读/写执行不能同时完成,因此应该只有一个活动的读或写异步操作。

对于读取,您只需调用async_read来启动读取序列,并在消耗接收到的数据后从读取处理程序中再次调用它。与在单线程环境中执行的操作相同。

对于写入,如果有并发生产者(如果多个线程提供要写入套接字的数据),则需要一个并发队列(例如增强循环缓冲区,查找“有界缓冲区示例”)。您的写入函数从此缓冲区获取数据并将其异步写入套接字。您的写入处理程序调用您的写入函数。

From Boost.Asio docs:

The io_service::strand class provides the ability to post and dispatch
handlers with the guarantee that none of those handlers will execute
concurrently.

There's a good example of strand usage in Boost.Asio examples.

strand guarantees that your handler execution will be synchronized. That means that strand is useful if your io_service is executed from multiple threads. It's not related to how you schedule your tasks (handlers).

strand cannot help you to do multiple socket read or write ops concurrently, because internal read/write execution cannot be done concurrently, so there should be just one active read or write async op.

For reads you just call async_read to initiate read sequence and call it again from your read handler after consuming received data. The same that you do in single threaded environment.

For writes if there're concurrent producers (if multiple threads provides data to be written to socket) you need a concurrent queue (e.g. boost circular buffer, look for "Bounded Buffer Example"). Your write function takes data from this buffer and async writes it to socket. Your write handler invokes your write function.

岁吢 2024-12-04 05:28:37

当我async_write()到多线程应用程序数据中的套接字时
可能被写入损坏。斯特兰德保证,这些都不会
处理程序将同时执行。

如果多个线程需要在套接字上写入数据,则必须确保数据的顺序。这在 async_write() 文档

程序必须确保流不执行其他写入操作
操作(例如 async_write、流的 async_write_some
函数或执行写入的任何其他组合操作)直到
至此操作完成。

我建议维护一个消息传出队列,这与这个问题和我的答案。

When I async_write() to the socket in multithreaded application data
may be written corrupted. And Strand guarantee that none of those
handlers will execute concurrently.

If multiple threads need to write data on the socket, you will have to ensure ordering of the data. This is explicitly clear in the async_write() documentation.

The program must ensure that the stream performs no other write
operations (such as async_write, the stream's async_write_some
function, or any other composed operations that perform writes) until
this operation completes.

I suggest maintain an outgoing queue of messages, which is very similar to this question and my answer.

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