Skip to content

Instantly share code, notes, and snippets.

@thedarkone
Created September 7, 2012 18:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thedarkone/3668383 to your computer and use it in GitHub Desktop.
Save thedarkone/3668383 to your computer and use it in GitHub Desktop.
This is not thread safe even on MRI
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