From: Robert Klemme Date: 2005-01-24T19:15:55+09:00 Subject: Re: nuby threading on threads "Assaph Mehr" schrieb im Newsbeitrag news:1106529293.644704.189240@c13g2000cwb.googlegroups.com... > Sounds like you need an implementation of thread-pool. > Read http://www.rubygarden.org/ruby?ObjectPoolingAndThreading - it > might help. Definitely. A common pattern is this require 'thread' PARALLEL = 10 TERMINATOR = Object.new # setup threads = [] queue = Queue.new PARALLEL.times do threads << Thread.new(queue) do |q| until ( TERMINATOR == ( obj = q.deq ) ) # do something with obj puts "#{Thread.current.inspect}: #{obj.inspect}" # or maybe deal with arbitrary code begin obj.call if obj.respond_to? :call rescue Exception => e $stderr.puts e end end end end # add tasks to queue 20.times {|i| queue.enq "Task 1.#{i}"} queue.enq lambda { puts "some arbitrary task"; sleep 2 } queue.enq lambda { puts "another task"; sleep 2 } queue.enq lambda { raise "Deadly!" } 5.times {|i| queue.enq "Task 2.#{i}"} # temination threads.each { queue.enq TERMINATOR } threads.each { |t| t.join } # end Kind regards robert