-
-
Save anonymous/014132240f0c9021bdbb to your computer and use it in GitHub Desktop.
threadsafe?
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
#!/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