Re: Completion ports and sockets - scaling my app

Tech-Archive recommends: Fix windows errors by optimizing your registry



Natscha Heumann wrote:
> Hi,
>
> I got some serious problems understanding the behaviour of
> socket-related iocps driven by multiple threads.
>
> I post some sends without waiting for their completion.
> Lets assume 2 chunks of data.
>
> First chunk 1 is put on his way by WSASend(). The send could not be
> satisfied in one call so only a part of the data could be sent and the
> rest has to be sended again.
>
> Thread 1 gets an IO-completion packet about the partial send and tries
> to send the rest. Right after receiving the completion packet for that
> send and before calling the WSASend for the rest of the data, the
> thread is preemted.
>
> The second thread sends chunk 2, so the data will be corrupted, won't
> they?

Yes it will. See the "Multithread safety" thread (pun not intended) from
November 4 in this group.
To summarize any data "stream" is not thread safe. It is your responsibility
to maintain stream integrity whether you use IOCP or not.

> So after considering this would it not be better to have only ONE
> outstanding send? Better wait for completion of one send before
> scheduling the next?

No. In general it is better to queue as many requests as you physically can.
To deal with serialization you can use either sequence numbers or arrange
your data chunks in a doubly linked-list.

> There is the great article in MSDN
>
> Windows Sockets 2.0: Write Scalable Winsock Apps Using Completion
> Ports by Anthony Jones and Amol Deshpande
> which states:

And it is entirely correct.

> How can I post multiple overlapped sends without corrupting my data?
> By implementing some kind of sequence numbering scheme?

See above.

--
Eugene
http://www.gershnik.com


.