tags:

views:

184

answers:

1

Hi all,

If am am calling boost::asio::async_write/async_read directly after each other, will the data be ordered? Or do I need to wait on the callback before I am calling write/read again?

Thanks in advance!

+2  A: 

The data is not guaranteed to be ordered and if you are using those functions you should wait for the callback before writing again.

(Discussion in terms of *async_write*, also applies to *async_read*)

Because *async_write* is implemented in terms of multiple calls to the underlying stream's *async_write_some* function, those calls are not atomic. Each call attempts to write data to the stream and has an internal callback to deal with partial operations, in effect waiting on completion as you might code yourself. So you could easily end up with mixed data if you don't wait for completion.

You also need to consider threads. If you call *async_x* on a stream multiple times you could end up with concurrent operations on the same underlying stream in different threads, leading to undefined behaviour.

janm