Thread safe socket code

J

jecheney

Hi,

Im currently using the following code for reading/writing to a network
socket.

private StreamReader clientStreamReader;
private StreamWriter clientStreamWriter;
....
TcpClient tcpClient = new
TcpClient(server_host_name, server_port);
NetworkStream clientSockStream =
tcpClient.GetStream();
clientStreamReader = new
StreamReader(clientSockStream);
clientStreamWriter = new
StreamWriter(clientSockStream);

I have been told using the StreamReader & StreamWriter classes is not
thread safe, and as i am about to implement threading i need to get
this fixed.

Is there a simple solution to make reading and writing to the stream
thread safe?

Thanks
Jack
 
P

Peter Duniho

[...]
I have been told using the StreamReader & StreamWriter classes is not
thread safe, and as i am about to implement threading i need to get
this fixed.

Is there a simple solution to make reading and writing to the stream
thread safe?

Maybe you can be more clear about what "thread safeness" it is you're
looking for.

"Thread safe" means that something can be used simultaneously from
multiple threads without a problem. However, using StreamRead and
StreamWriter with the TcpClient, even if you synchronized access to the
reader and writer across multiple threads, that's not usually what one
would want to do. Making the use of the objects "thread safe" is simple
enough, but if you have more than one thread reading from the same stream
or more than one thread writing to the same stream, you may run into all
sorts of other problems (mostly to do with coordinating what each thread
reads or writes).

If you are adding new threads to your design, but those new threads are
not actually going to access the stream and/or the reader and writer, then
they are already "thread safe" enough. That is, they aren't really thread
safe but you also aren't doing anything that would cause any problems in
that respect.

(Note: using the lower-level API, Winsock, it is fine to read in one
thread and write in another thread using the same socket. I don't know
whether the NetworkStream class inherits this behavior or not; it's
possible that the documentation for the class says one way or the other,
but I haven't had a chance to review it to see. It's probably safest to
assume that you can't use the NetworkStream from more than one thread at a
time, even if it's a matter of reading in one thread and writing in
another. But it's possible that requirement doesn't actually exist and
that all you really need to worry about is not reading in more than one
thread at a time and not writing in more than one thread at a time).

The fact is that most of the .NET classes aren't thread safe. If a .NET
class (or some subset of the class) is thread safe, usually it's just
because the work that the class does is inherently thread safe. And yet,
multi-threaded .NET programs can often be written without any explicit
synchronization happening, simply because the threads are actually doing
different things and not trying to access the same data.

So maybe if you can elaborate on how it is you are using the streams and
whether they are actually going to be used from different threads
simultaneously, that would help in providing you the best answer.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top