Calling boost ASIO async_receive() again before a previous one has completed

时间:2017-06-22 15:25:47

标签: c++ boost boost-asio asio

I know that concurrent calls of methods of the same socket object leads to undefined behaviour.

But what about calling an asynchronous operation and calling it again (non concurrently) before the completion handler of the first one is invoked?

Say, what is the expected behaviour of the following (if any):

boost::asio::ip::udp::socket socket;

// make socket join a multicast group, for instance

socket.async_receive( boost::asio::null_buffers( ) , & handler1 );

// assume handler1() is not called between this two lines

socket.async_receive( boost::asio::null_buffers( ) , & handler2 );

?

This seems something someone should not be doing, but I couldn't find in the docs any specific place where such an issue is addressed.

1 个答案:

答案 0 :(得分:2)

你是对的,这在异步i / o中是一个很大的禁忌。两次调用async_receive确实没什么意义,但是为了避免这种情况,发送时可能意味着使用某种队列,一个boost :: circular_buffer,或类似的东西。这完全取决于您的申请。

我认为问题没有解决,因为boost文档假设读者已经熟悉使用异步套接字。