Can starting multiple asyncronous read/write operations on the same Stream corrupt the data?
Asked Answered
B

2

6

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...

Bihar answered 20/5, 2011 at 20:24 Comment(4)
Does it not matter in which order the 3 buffers are sent? What if the data is sent as 222233331111, ie. no interleaving but "incorrect" order?Caravel
Yup, similar question: #1541158Ked
I guess you shouldn't do that. MSND states about the Stream object - Any instance members are not guaranteed to be thread safe.Prosector
Be sure to read support.microsoft.com/default.aspx?scid=kb;en-us;156932, particularly the part that says, "Another reason that I/O operations are completed synchronously is the operations themselves. On Windows NT, any write operation to a file that extends its length will be synchronous."Commonweal
H
8

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:

Both send and receive operations can be overlapped. The receive functions may be invoked multiple times to post receive buffers in preparation for incoming data, and the send functions may be invoked multiple times to queue up multiple buffers to be sent. Note that while a series of overlapped send buffers will be sent in the order supplied, the corresponding completion indications may occur in a different order. Likewise, on the receiving side, buffers will be filled in the order they are supplied, but completion indications may occur in a different order.

Not surprisingly, the same guarantees carry over to the managed side of the world, eg. the NetworkStream class:

Read and write operations can be performed simultaneously on an instance of the NetworkStream class without the need for synchronization. As long as there is one unique thread for the write operations and one unique thread for the read operations, there will be no cross-interference between read and write threads and no synchronization is required.

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.

Handball answered 20/5, 2011 at 21:55 Comment(1)
So basically if I lock the NetworkStream while calling BeginWrite I won't have problems and concurrent running async operations won't conflict with each other.Bihar
C
0

No, file streams do not support multiple concurrent IOs. The Windows file system does not handle that well. It will almost certainly throw an exception.

Edit:

Synchronization and Overlapped Input and Output seems to indicate that the file system will correctly handle multiple overlapped IO requests. My bad.

Do not, however, try to do concurrent synchronous writes on a FileStream. That throws an exception.

Commonweal answered 20/5, 2011 at 21:48 Comment(4)
-1 There is ample documentation on MSDN that proves the contraryHandball
@Remus: please provide a link.Commonweal
There are several links in my responseHandball
@Remus: All of which mention Windows Sockets, not the file API. I'll go look.Commonweal

© 2022 - 2024 — McMap. All rights reserved.