开发者

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

开发者 https://www.devze.com 2023-01-24 05:05 出处:网络
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 c

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?


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消