Mutual exclusion and C connectors

I am maintaining an existing system where previous developers in each operation are done on a socket that requires multiple threads to read and write, previous developers have done io operations under control and a mutex. is there a need for mutual exclusion of C socket I / O? Or, since sockets are full duplex, is using a mutex redundant? Only one stream

There is no doubt in my mind that the processing queue to which the thread is placing the object in this shared memory, and care must be taken for mutual exclusion.

0


a source to share


2 answers


Sockets are not thread safe by default. Therefore, if you have multiple read and write threads to them, you need to block access in some way (for example, using a mutex).



+1


a source


in the case of TCP (AF_INET, SOCK_STREAM), it is normal for it to have a read (recv) stream and a thread (send) stream that are out of sync.

But it is not clear from your description for what purpose the mutex is used in your code - it looks like synchronized network operations "by previous developers" not because of sockets, but because of the requirements of your application protocol. Many applications communicate as follows:



lock
-> send request
<- recv reply
unlock

lock
-> send request
<- recv reply
unlock

      

a blocking is required here (if multiple threads are involved) to keep the send / recv pairs in sync, otherwise your application protocol can become a jumble of unmatched requests and responses.

0


a source







All Articles