Revisions
-
manveru revised this gist
Jun 5, 2013 . 2 changed files with 16 additions and 20 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,20 +0,0 @@ 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,16 @@ #!/usr/bin/env ruby threads = 100.times.map do |n| Thread.new n do |m| base = 1 + (m * 100) 100.times.map do |i| base + i end end end result = threads.reduce(0) do |sum, thread| sum + thread.join.value.reduce(:+) end puts "The multi-threaded result is: #{result}" -
jcoene created this gist
Feb 25, 2013 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,20 @@ #!/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}"