Skip to content

Instantly share code, notes, and snippets.

@manveru
Forked from jcoene/gist:5034155
Created June 5, 2013 20:41
Show Gist options
  • Save manveru/5717144 to your computer and use it in GitHub Desktop.
Save manveru/5717144 to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby
# This program adds up the numbers 1 through 10,000 using 100 threads.
# Challenge A: Does this solution have any problems? If so, explain.
# Challenge B: Propose an alternate implementation that does not use threads.
require "thread"
mt_result = 0
100.times.map do |n|
Thread.new do
base = 1 + (n * 100)
100.times do |i|
mt_result += base + i
end
end
end.map(&:join)
puts "The multi-threaded result is: #{mt_result}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment