From: matz@... (Yukihiro Matsumoto) Date: 2000-10-03T15:25:08+09:00 Subject: [ruby-talk:5252] Re: Running two iterators in parallel Hi, In message "[ruby-talk:5245] Re: Running two iterators in parallel" on 00/10/03, Yukihiro Matsumoto writes: ||I have two iterators. I would like to run them in parallel, creating ||an iterator that generates arrays containing one element from each. ||Can anyone solve this puzzle? | |Use threads. Oops. There is a termination condition bug. Try this instead. matz. --- require 'thread' def combine(*args) queues = [] threads = [] for it in args queue = SizedQueue.new(1) th = Thread.start(it, queue) do |i,q| self.send(it) do |x| q.push x end end queues.push queue threads.push th end loop do ary = [] for q in queues ary.push q.pop end yield ary for th in threads return unless th.status end end end public :combine def it1 () yield 1; yield 2; yield 3 end def it2 () yield 4; yield 5; yield 6 end combine('it1','it2') { |x| # x is (1, 4), then (2, 5), then (3, 6) }