async_write - 提升。它会复制缓冲区吗?

发布于 2024-10-07 12:51:10 字数 269 浏览 0 评论 0原文

async_write(*this, BoostAsio::buffer(pck->_storage), boost::bind(&tcp_socket::handle_wrote, this, pck, BoostAsio::placeholders::error));

pck 是在堆上分配的,如果我删除它,_storage 也会变得无效,还是 async_write 将缓冲区复制到其内部结构中,并且可以在堆栈上自由删除/分配?

谢谢。

async_write(*this, BoostAsio::buffer(pck->_storage), boost::bind(&tcp_socket::handle_wrote, this, pck, BoostAsio::placeholders::error));

pck is allocated on heap, if I delete it, would _storage become invalid as well or does async_write copy the buffer into its internal structures and it can be freely deleted/allocated on stack?

Thank you.

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

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

发布评论

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

评论(2

何以心动 2024-10-14 12:51:11

async_write 函数不会复制缓冲区。它传递一个内部 const_buffer 反对更深层次的库函数。 const_buffer 对象包含一个指向实际缓冲区的内部指针,因此不会执行深度复制。您需要确保 pck->storage 在调用异步处理程序之前保持有效。

请注意,这当然是最好的。每次复制缓冲区都会造成很大的性能消耗。

The async_write function does not copy the buffer. It passes an internal const_buffer object to deeper library functions. The const_buffer object contains an internal pointer to the actual buffer, so no deep copy is performed. You need to make sure that pck->storage remains valid until after the asynchronous handler is invoked.

Note that this is certainly for the best. Copying the buffer every time would be a really big performance drain.

薄凉少年不暖心 2024-10-14 12:51:11

请注意缓冲区或至少其中一部分将被复制到内核的套接字缓冲区中。通常这没什么大不了的。但是,您可以将发送缓冲区设置为零,以便在操作期间使用应用程序的缓冲区。当然,在不了解禁用发送缓冲区的其他后果的情况下,您不应该这样做。

Note the buffer or at least some of it will be copied into the kernel's socket buffer. Usually this is not a big deal. However You can set the send buffer to zero to use the application's buffer during the operation. Of course you shouldn't do that without understanding the other consequences of disabling the send buffer.

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