I’m using asynchronous I/O because it does not block the calling thread and does the threading stuff behind the scenes. If I invoke multiple async operations like BeginWrite()’s on the same Stream, should I need to worry about that data buffer contents are mixed together?
Let’s assume I want to send 3 buffers:
Buffer1: 1111111111
Buffer2: 2222222222
Buffer3: 3333333333
I don’t mind if the buffers are sent in incorrect order, so
333333333311111111112222222222
is ok, but is it possible that buffer contents are got completely mixed together?
122213121212122333313111223333
PS: I’m 100% sure that someone already asked this in some form…
It depends on the implementation of the Stream. For instance sockets support multiple overlapped requests both for read and for write, and so does the file API. They guarantee consistency of each operation (no interleaving content) and also order of operations: for instance for socket reads the bytes received will be placed in the buffers posted in the order posted. If these guarantees would not be provided it would impossible to write high performance network application, as overlapping Sends are desired but overlapping Receives are actually required for high performance network IO. This behavior is described in many articles, including good ole’ Windows Sockets 2.0: Write Scalable Winsock Apps Using Completion Ports and is documented on MSDN Overlapped Input/Output:
Not surprisingly, the same guarantees carry over to the managed side of the world, eg. the
NetworkStreamclass:That being said, throwing randomly async reads and writes on a Stream will result in chaos pretty quickly. Applications need to carefully orchestrate the order in which threads submit the operations so that the order is deterministic. Usually this means keeping accounting in a (synchronized) list with special care to do the async operation invocation while holding the synchronization lock.
One final note is that all these async APIs make a special note in calling out that the order of completion is not guaranteed to match the order of submission.