Say first I call async_connect on a newly constructed tcp::socket. Before the handler of async_connect is called, I call async_read/writes on the same socket. Will them wait for the connection or fail immediately because the socket has not connected?

有帮助吗?

解决方案

Since async_connect performs asynchronously, your scenario has race-condition - so it may occasionally work or fail, depending on timings. In order to begin async. i/o on a socket, one should wait first for async_connect completion handler - just like described in the Asio documentation.

That's said, you can design a class that will accept async. operations as functors, store them in a queue, and internally chain/invoke them in the correct order.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top