From: Brian Candler Date: 2009-12-16T18:29:29+09:00 Subject: Re: Using threads to show progress Piyush Ranjan wrote: > In the enumerate thread keep pushing id to queue whenever there is a new > object being worked on. > > dictated_exams.each do |exam| > queue.push exam.id > end That's a really good suggestion: the consumer will block when it tries to pop from the queue, so you don't need to spin. However in that case you can't also poll Thread#alive?, so I suggest you wrap the producer thread to make it send a termination message, even if it terminates abnormally with an exception. require 'thread' dictated_exams = ["foo","bar"] queue = Queue.new enumerate = Thread.new do begin dictated_exams.each do |exam| queue.push exam raise "Oops" if exam == "baz" end ensure queue.push nil end end while current = queue.pop puts current end enumerate.join # just to propagate exception -- Posted via http://www.ruby-forum.com/.