views:

167

answers:

2

I am maintaining an existing system where previous developers on each operation is performed on the socket, to which multiple threads are required to read and write to, the previous developers have performed the io operations under the control and a mutex. is there a requirement to mutually exclude C socket IO operations? Or since sockets are full duplex, the use of a mutex is redundant? Only one thread

There is no question in my mind that the processing queue to which the thread puts an object into is shared memory and care must be taken to mutually excluse it.

+1  A: 

Sockets are not thread-safe by default. So if you have multiple threads reading and writing to them you will need to lock access in some way (e.g. with a mutex).

Matthew Murdoch
A: 

in case of TCP (AF_INET, SOCK_STREAM) it is OK to have a reader thread (recv) and a writer thread (send) which are not synchronized.

But from your description it is not clear for what purpose mutex is used in your code - it looks like "previous developers" synchronized networking operations not because of sockets, but due to requirements of your application protocol. Many applications perform communications this way:

lock
-> send request
<- recv reply
unlock

lock
-> send request
<- recv reply
unlock

locking is required here (if multiple threads are involved) to synchronize send/recv pairs, otherwise your application protocol might become a mess of unmatched requests and replies.

Andrey