Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Synchronize keyword is not present in Ruby. Alternatively just can just wrap the method call to <code>Mutex</code> (i.e. fancy word for Lock).</p> <p>Create new shared Mutex for that class (everyone must use the same Mutex (lock) to access the same variables):</p> <pre><code>NUM_THREADS = 4 class Foo def initialize @my_mutex = Mutex.new @my_val = 0 # should be private end def synchronize(&amp;block) # to see what it does without the mutex in this example: # 1) comment this line @my_mutex.synchronize(&amp;block) # 2) uncomment this line # yield end def current_value synchronize do @my_val end end def modify # the value should be 0 before and 0 after, if the Mutex is used correctly synchronize do @my_val += 1 sleep 0.25 @my_val -= 1 sleep 0.25 end end end foo = Foo.new threads = [] # spawn N threads, all trying to change the value threads += (1..NUM_THREADS).map { |i| Thread.new { puts "thread [##{i}]: modifying" foo.modify } } # spawn checking thread threads &lt;&lt; Thread.new { # print the value twice as fast as the other threads are changing it, so we are more likely to stumble upon wrong state (NUM_THREADS * 2).times { puts "thread [check]: checking..." raise if foo.current_value != 0 # locking failed, crash sleep 0.25 } } threads.map { |t| t.join } # wait for all threads puts "even though it took a while longer, it didn't crash, everyone is happy, managers didn't fire me... it worked!" </code></pre> <p>See <a href="http://apidock.com/ruby/Mutex" rel="nofollow noreferrer">http://apidock.com/ruby/Mutex</a></p> <p>The program runs longer, because of all those locking. Speed depends on your ruby implementation (e.g. green threads, native threads..) and number of cores. If you disable mutex in this example above, the program crashes right away, because of the raise guard in checking thread. Note that the checking thread must use the mutex too, because otherwise it would still be able to read the value in the middle of change by other threads. I.e. everyone must use the same mutex to access that variable.</p> <p>To make around the lack of <code>synchronized</code> keyword, I defined method <code>synchronize</code> which uses the class defined <code>Mutex</code>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload