I have the following code (from a Ruby tutorial):
require 'thread'
count1 = count2 = 0
difference = 0
counter = Thread.new do
loop do
count1 += 1
count2 += 1
end
end
spy = Thread.new do
loop do
difference += (count1 - count2).abs
end
end
sleep 1
puts "count1 : #{count1}"
puts "count2 : #{count2}"
puts "difference : #{difference}"
counter.join(2)
spy.join(2)
puts "count1 : #{count1}"
puts "count2 : #{count2}"
puts "difference : #{difference}"
It’s an example for using Mutex.synchronize. On my computer, the results are quite different from the tutorial. After calling join, the counts are sometimes equal:
count1 : 5321211
count2 : 6812638
difference : 0
count1 : 27307724
count2 : 27307724
difference : 0
and sometimes not:
count1 : 4456390
count2 : 5981589
difference : 0
count1 : 25887977
count2 : 28204117
difference : 0
I don’t understand how it is possible that the difference is still 0 even though the counts show very different numbers.
The add operation probably looks like this:
val = fetch_current(count1)
add 1 to val
store val back into count1
and something similar for count2. Ruby can switch execution between threads, so it might not finish writing to a variable, but when the CPU gets back to the thread, it should continue from the line where it was interrupted, right?
And there is still just one thread that is writing into the variable. How is it possible that, inside the loop do block, count2 += 1 is executed much more times?
Execution of
takes some time (although it may be short). It is not done in an instance. Therefore, it is not mysterious that the two consecutive lines:
are showing different counts. Simply, the
counterthread went though some loop cycles and incremented the counts while the firstputswas executed.Similarly, when
is calculated, the counts may in principle increment while
count1is referenced beforecount2is referenced. But there is no command executed within that time span, and I guess that the time it takes to refer tocount1is much shorter than the time it takes for thecounterthread to go through another loop. Note that the operations done in the former is a proper subset of what is done in the latter. If the difference is significant enough, which means thatcounterthread had not gone through a loop cycle during the argument call for the-method, thencount1andcount2will appear as the same value.A prediction will be that, if you put some expensive calculation after referencing
count1but before referencingcount2, thendifferencewill show up: