Created
September 7, 2012 18:27
-
-
Save thedarkone/3668383 to your computer and use it in GitHub Desktop.
This is not thread safe even on MRI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'thread' | |
bignum = (2 ** (0.size * 8)) | |
start = 0 | |
h = {:a => start} | |
h[:a] += bignum # get into the Bignum territory | |
iterations = 100_000 | |
thread_count = 100 | |
threads = (1..thread_count).map do | |
Thread.new do | |
iterations.times do | |
h[:a] += 1 # 1 | |
h[:a] += 1 # 2 | |
h[:a] += 1 # 3 | |
h[:a] += 1 # 4 | |
h[:a] += 1 # 5 | |
h[:a] += 1 # 6 | |
h[:a] += 1 # 7 | |
h[:a] += 1 # 8 | |
h[:a] += 1 # 9 | |
h[:a] += 1 # 10 | |
end | |
end | |
end | |
threads.map(&:join) | |
h[:a] -= bignum | |
puts "h[:a] => #{h[:a]}, should be: #{(thread_count * iterations * 10)}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment