Re: Socket Send and Receive Thread Safety

From: Eugene Gershnik (gershnik_at_hotmail.com)
Date: 02/24/05


Date: Wed, 23 Feb 2005 16:44:35 -0800

Nizar wrote:
> Is it safe for one thread to call Socket.Receive and another to call
> Socket.Send on the same Socket at the same time?

Should be fine unless .NET library "designers" screwed things up completely.

> The documentation says that instance members are not guaranteed to be
> thread safe, but I'm not sure whether this combination is or not.

This is just a general warning for all .NET crap^H^H^H^H classes that tells
you that they don't do this:

public some_method()
{
    lock(this);
    ....
    unlock(this);
}

in every method. Once upon a time in a land called Java class library
something like the code above was considered a good idea. They got smarter
since.
What it means that it is not a good idea to call methods that _modify_
socket (so called mutating methods) simultaneously with other methods
without explicit locking. Recv and Send are usually considered non-mutating
methods so they should be fine. In better languages there is a little word
"const" that tells you which methods are mutating.

> I've also heard that the send and receive buffers are disparate and
> can be accessed simultaneously.

Should be the case for anything usefull.

--
Eugene


Relevant Pages

  • Re: [OT] how to clean grime off old computer MB?
    ... I don't want to remove e.g. the CPU from its socket. ... You need to remove all socketed devices, and especially the CPU. ... which presumably is safe for the ... believe was safe for aluminum electrolytics; ...
    (Debian-User)
  • Re: Read from and Write to the same socket from two threads
    ... The FAQ page means the following: ... Anecdotal evidence suggests that one thread calling sendand another thread calling recvon a single socket is safe on recent Microsoft stacks at least. ...
    (microsoft.public.win32.programmer.networks)
  • Re: Read from and Write to the same socket from two threads
    ... Anecdotal evidence suggests that one thread calling sendand another ... thread calling recv() on a single socket is safe on recent Microsoft ... and write to the same socket in another thread. ...
    (microsoft.public.win32.programmer.networks)
  • Re: Read from and Write to the same socket from two threads
    ... I've consulted your question and yes it's safe to use sendin one thread ... and recv() in another thread for the same socket since all socket calls are ... This posting is provided "AS IS" with no warranties, and confers no rights. ...
    (microsoft.public.win32.programmer.networks)