Created
September 6, 2011 04:44
-
-
Save ebisawa/1196608 to your computer and use it in GitHub Desktop.
parallel_each
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
module Enumerable | |
DEFAULT_PARALLEL = 8 | |
def parallel_each(num = DEFAULT_PARALLEL) | |
threads = [] | |
queue = Queue.new | |
num.times do | |
threads << Thread.new do | |
while (a = queue.pop) != nil | |
yield(a) | |
end | |
end | |
end | |
self.each {|a| queue << a } | |
self.size.times { queue << nil } | |
threads.each {|t| t.join } | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment