async_write - 提升。它会复制缓冲区吗?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
async_write
函数不会复制缓冲区。它传递一个内部const_buffer
反对更深层次的库函数。 const_buffer 对象包含一个指向实际缓冲区的内部指针,因此不会执行深度复制。您需要确保pck->storage
在调用异步处理程序之前保持有效。请注意,这当然是最好的。每次复制缓冲区都会造成很大的性能消耗。
The
async_write
function does not copy the buffer. It passes an internalconst_buffer
object to deeper library functions. Theconst_buffer
object contains an internal pointer to the actual buffer, so no deep copy is performed. You need to make sure thatpck->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.
请注意缓冲区或至少其中一部分将被复制到内核的套接字缓冲区中。通常这没什么大不了的。但是,您可以将发送缓冲区设置为零,以便在操作期间使用应用程序的缓冲区。当然,在不了解禁用发送缓冲区的其他后果的情况下,您不应该这样做。
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.