提高ASIO缓冲懒分配 [英] boost asio buffer lazy allocation
问题描述
异步操作。
现在我通过preallocated字节的缓冲区,例如:
Now I pass preallocated byte buffer, for example:
s.async_receive_from(
boost::asio::buffer( preallocated_pointer, preallocated_size ),
_remote_endpoint,
boost::bind(...)
);
时有可能使懒惰分配,这和其他电话?
Is it possible to make lazy allocation for this and other calls?
推荐答案
当需要资源分配懒惰,或者分配,可以使用实现<一个href=\"http://www.boost.org/doc/libs/1_52_0/doc/html/boost_asio/reference/null_buffers.html\"><$c$c>boost::asio::null_buffers$c$c>. null_buffers
可用于内Boost.Asio的获得反应堆式操作。这可以通过使用共享内存池与第三方库集成是有用的,等等。Boost.Asio的<一个href=\"http://www.boost.org/doc/libs/1_52_0/doc/html/boost_asio/overview/core/reactor.html\">documentation提供了一些信息和下面的例子code:
Lazy allocation, or allocating when the resource is needed, can be accomplished using boost::asio::null_buffers
. null_buffers
can be used to obtain reactor-style operations within Boost.Asio. This can be useful for integrating with third party libraries, using shared memory pools, etc. The Boost.Asio documentation provides some information and the following example code:
ip::tcp::socket socket(my_io_service);
...
socket.non_blocking(true);
...
socket.async_read_some(null_buffers(), read_handler);
...
void read_handler(boost::system::error_code ec)
{
if (!ec)
{
std::vector<char> buf(socket.available());
socket.read_some(buffer(buf));
}
}
这篇关于提高ASIO缓冲懒分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!