Re: Multithread safety



Alun Jones wrote:
> If your protocol is TCP, then yes, you need to reassemble the read
> results.

This whole discussion was about TCP

> TCP is, by its nature, a serial protocol per-socket - as such, it
> makes very little sense to consider multiple threads reading from the
> same socket at the same time. Multiple threads writing to the same
> socket - now that is something that might make sense, but there are
> some caveats there.

We are talking about IOCP here. The WSARecv or WSASend are not reads or
writes but rather queued requests. The actual reading is dome by the thread
pool calling GetQueuedCompletionStatus and there can certainly be more than
1 of them. As for WSARecv/WSASend the way they are posted depends on the
protocol and application logic. Except for one case with UDP I always had
them in one thread but these were quirks of what I had to deal with. A
general TCP server may find it convenient to post from multiple threads.

> You should generally use a mutex to prevent two
> writes on the same socket from interfering.
>
> This is true even for an overlapped socket, because the WSASend()
> calls might find their data is interleaved.
>
> I know, not what you wanted to hear - you wanted to hear that
> Microsoft had done this work for you. Sadly, no.

Not really. You may want to re-read this thread from the begining to see
that I was making exactly the same points. The confusion in was about IOCP
case specifically. What would be nice for MS to do is to have
GetQueuedCompletionStatusEx return the sequence number of the completion in
the queue. In this case it would be possible to avoid locking on
WSARecv/WSASend. See the nearby subthread for detials.


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


.



Relevant Pages

  • HEADS UP: network stack and socket hackery over the next few weeks
    ... Over the next few weeks, I'll be doing a fairly serious workworking of the socket and protocol reference models, in order to clean up a number of long-standing race conditions and provide infrastructure for significant locking optimizations for several protocols (including TCP). ...
    (freebsd-current)
  • Re: Letting a Python application phone home
    ... You need to define a protocol for the communication ... Look at the python module "socket" resp. ... "SocketServer" for low level tcp functions. ... let your program contact the server periodically. ...
    (comp.lang.python)
  • Re: Reading blocks of data from socket
    ... I have an application that connects to server via TCP. ... way the protocol is designed, I read a few bytes initially from the ... socket, say 'n'. ... let the program logic interpret the packets in-memory rather than from ...
    (microsoft.public.win32.programmer.networks)
  • Re: Reading blocks of data from socket
    ... I have an application that connects to server via TCP. ... way the protocol is designed, I read a few bytes initially from the ... socket, say 'n'. ... let the program logic interpret the packets in-memory rather than from ...
    (microsoft.public.win32.programmer.networks)
  • Re: TCPIP Default keep alive question
    ... since last December we have been getting ASOD abends in certain CICS ... Did you mean to alert us to the possibility that the *partner* TCP ... SO_KEEPALIVE socket option and do not override the interval using the ... TCP keepalive probes end TCP connections after a period of inactivity. ...
    (bit.listserv.ibm-main)