Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mutithread socket writing is safe without mutex

I am using a socket and this will be use in multithread environment where many thread will write on same socket. Does socket it-self synchronize it or I have to use mutex?
Will one trher will be block if another one is writing on it?

like image 749
CrazyC Avatar asked May 18 '26 02:05

CrazyC


1 Answers

For single write it is atomic. The catch is message ordering (including interleaved message) at receiving side. If it is needed then you need locking. If you don't want to spend too much time on the lock. You can use producer/consumer pattern to decrease locking time.

like image 155
fadedreamz Avatar answered May 20 '26 16:05

fadedreamz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!