Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thread safety in ruby

I have a variable that is accessed from two threads. Do I need to use mutex when one thread is modifying it while the other is only reading it?

My understanding is that mutex is needed if both threads are writing to the same variable, but not for the case above as write happens only from one thread.

like image 520
GoT Avatar asked Jul 05 '26 23:07

GoT


1 Answers

Unless the method modifying the object is atomic, you need mutex. In fact, even write is not atomic in Ruby. Cf. Read and write file atomically.

like image 79
sawa Avatar answered Jul 07 '26 13:07

sawa



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!