3

Let's assume I have hash like:

@@foo = { 1 => 2, 2 => 3, 3 => 4}

If I want to change value of key 1 do I have to use Mutex that it's used for whole read/write protection or Mutex object which is defined for key 1 write/reads ?

I could not find detailed concept apart of that using Mutex is necessary.

Dawid Gosławski
  • 2,028
  • 1
  • 18
  • 25
  • possible duplicate of [Thread Safety: Class Variables in Ruby](http://stackoverflow.com/questions/9558192/thread-safety-class-variables-in-ruby) – Wand Maker Sep 26 '15 at 07:53
  • @WandMaker it's not the question for general thread safety but rather safety if parallel access to different parts of hash – Dawid Gosławski Sep 26 '15 at 11:51
  • Mutation of Hash is same as Mutation of any other class level object....right? – Wand Maker Sep 26 '15 at 11:57
  • Not really - if you eg mutate string there is a big chance to mess up without mutex. But if every thread modify only one key in hash - is there any chance to break anything? – Dawid Gosławski Sep 26 '15 at 14:32
  • what you need is a read-write lock http://ruby-concurrency.github.io/concurrent-ruby/master/Concurrent/ReentrantReadWriteLock.html – Oshan Wisumperuma Jan 20 '19 at 16:25

0 Answers0