user2589714
user2589714

Reputation: 1

Is the lock necessary when a host attempts to receive the data from different sockets

I have three machines A, B, and C that are all connected each other. If A and B try to send data to C simultaneously, Can C use two different threads to receive the respective data without using any locks? Here C is connected to A and B through different sockets. Thanks in advance.

Upvotes: 0

Views: 21

Answers (2)

rockstar
rockstar

Reputation: 3538

[posting my comment as answer as it is not wrong and makes sense :P even referenced.]

I would say that you can have 2 threads . One thread listening for data from socket 1 and the other thread listening for data from socket 2 .

But if you need a lock or not should depend on what you do with the data . Do you write it to some buffer ? Since threads share Data,Code & Heap segment therefore you must be careful when you write this received data in which case you need to lock .

This is my basic understanding . I shall wait for more knowledgeable answers here.

Upvotes: 0

Martin James
Martin James

Reputation: 24907

Well, yes - no explicit locks anyway. The IP stack will have its own internal locks, but I don't think that's what you are asking.

You already appreciate that multiple processes can communicate simultaneously with different servers, and multiple processes implies different threads. The IP stack is therefore thread-safe.

Given the usual general care with any shared data inside one multithreaded process, (as metioned by rockstar comment), there is no problem with those threads communicating with IP endpoints on different peers/hosts. This is very common and works fine.

The two threads on C can safely communicate independently with A and B.

Go ahead - try it!

Upvotes: 1

Related Questions