Mutual Exclusion not happened in Ruby -


program:

def inc(n)     n + 1 end  sum = 0 threads = (1..10).map     thread.new         10_000.times             sum = inc(sum)         end     end end threads.each(&:join) p sum 

output:

$ ruby mutualexclusion.rb  100000 $  

my expected output of above program less 100,000. because, above program create 10 threads , each of thread update shared variable 'sum' 10,000 times. during execution of program, mutual exclusion happen. because, mutual exclusion not handled here. expect less 100,000 output. gives 100,000 output. how happened ? handle mutual exclusion here ? , how experiment problem(me).

the default interpreter ruby (mri) doesn't execute threads in parallel. mechanism that's preventing race condition introducing casually unexpected behavior global interpreter lock (gil).

you can learn more this, including similar demonstration, here: http://www.jstorimer.com/blogs/workingwithcode/8085491-nobody-understands-the-gil


Comments

Popular posts from this blog

AbotX : How do you create a parallel crawler that stays on and can be added to at run time from new requests -

testing - Detect whether test has failed within fixture -

android - Create single AAR file from multiple modules using Gradle -