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

sql - invalid in the select list because it is not contained in either an aggregate function -

Angularjs unit testing - ng-disabled not working when adding text to textarea -

How to start daemon on android by adb -