I have a socket which I receive from and send to in a single thread. However, there exists another thread which can periodically use the socket to send data. Currently, I'm using a shared object to lock my Socket.send()
operations. Is this lock necessary? If yes, would I need the lock on my receive method as well even though I'm only receiving from one thread?
Eg in Thread1:
//...some code
while (offset< len)
{
currentBytesRead += Client.Receive(buf, offset, len - offset, SocketFlags.None);
}
//...some more code...
lock (lockObject)
{
Client.Send(outputByte);
}
In Thread2:
lock (lockObject)
{
Client.Send(outputByte);
}