Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top