boost asio buffer lazy allocation

∥☆過路亽.° 提交于 2019-12-03 21:49:26

问题


Async operations.

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?


回答1:


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));
  }
}


来源:https://stackoverflow.com/questions/14204901/boost-asio-buffer-lazy-allocation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!