Rella
Rella

Reputation: 66965

What happens when writing to a socket from different threads at the same time?

I have two threads, each with a buffer with some data. I have a function that writes to a TCP socket. (I use ffmpeg for it.) How will the data packets arrive at the server — in some crazy mixed up way, or in some other order, or will the OS (Windows, in my case) make one thread wait until the other has finished?

Upvotes: 1

Views: 398

Answers (2)

crsuarezf
crsuarezf

Reputation: 1271

mmm, well, you have to synchronize them, to avoid unexpected results.

Upvotes: 0

Billy ONeal
Billy ONeal

Reputation: 106599

That depends entirely upon which implementation of "sockets" you are using. If your implementation is synchronized, you'll get the first thread's data, followed by the second thread's data (The second thread will block while the first sends). If your implementation is not synchronized, you'll get gibberish.

Upvotes: 5

Related Questions