Skip to content

Instantly share code, notes, and snippets.

Created March 11, 2016 18:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/014132240f0c9021bdbb to your computer and use it in GitHub Desktop.
Save anonymous/014132240f0c9021bdbb to your computer and use it in GitHub Desktop.
threadsafe?
#!/usr/bin/env ruby
class Foo
def initialize level
@level = level
end
def decrease it
@level[it] -=1
end
def [](it)
@level[it]
end
end
f = Foo.new(a: 1000, b: 200, c: 300)
## start 10 threads and use them to redice by 10 ==> 10 * 10 = 100
puts "Before"
p f
t = Array.new
100.times {
t<< Thread.new{
10.times { f.decrease(:a) }
p f[:a]
}
}
t.each(&:join)
puts "After"
p f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment