Reputation: 857
I'm using a StreamReader to read from a TcpSocket, and a StreamWriter to write to that TcpSocket. I would like to have the reading code in one thread and the writing code in another. How do I do this in a thread-safe manner?
If I do the following, then I guess the writer thread will be blocked in many cases:
lock (tcpClient) {
streamReader.ReadLine();
}
Here would be the writer code:
lock (tcpClient) {
streamWriter.WriteLine(line);
}
Upvotes: 2
Views: 2379
Reputation: 217233
It is safe to send on one thread and receive on the other thread.
So you can do this in one thread:
streamReader.ReadLine();
and this in another thread:
streamWriter.WriteLine(line);
without having to lock the tcpClient
.
Upvotes: 4