Created
January 22, 2014 21:33
-
-
Save laser/8567763 to your computer and use it in GitHub Desktop.
Multithread unsafe
This file contains hidden or 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
def add_range_unsafe | |
c = StatefulCalculator.new | |
threads = (1..256).each_slice(16).map do |range| | |
Thread.new do | |
range.each { |n| c.add n } | |
end | |
end | |
threads.map(&:join) | |
c.result | |
end | |
puts add_range_unsafe # 24399 | |
puts add_range_unsafe # 26126 | |
puts add_range_unsafe # 25674 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment