Prev: ruby bounties--list of bounties
Next: New gem snmpstats
From: Lune Lune on 22 Jan 2010 05:17 Hi Robert Thanks for your example. I wanted to play with it to really understand how that works. So I tried this : QUEUE = Queue.new threads = (1..5).map do |a| Thread.new(QUEUE) do |q| until ( q == (task = q.deq)) print "thread-", a, " ", task.inspect, "\n" end end end 10.times do |t| puts "t #{t}" 30.times { |s| puts "s #{s}"; QUEUE.enq(s) } threads.size.times { QUEUE.enq QUEUE } threads.each {|t| t.join} end In my loop "30 times" 5 * 6 threads are created. But when I add "10 times" I should see 10 * what I had earlier. This is not happening. I have my 5 * 6 threads created and that's all. Could you explain why ? Thanks -- Posted via http://www.ruby-forum.com/.
From: Jesús Gabriel y Galán on 22 Jan 2010 05:33 On Fri, Jan 22, 2010 at 11:17 AM, Lune Lune <verduc_m(a)epitech.net> wrote: > Hi Robert > > Thanks for your example. > > I wanted to play with it to really understand how that works. So I tried > this : > QUEUE = Queue.new > threads = (1..5).map do |a| > Thread.new(QUEUE) do |q| > until ( q == (task = q.deq)) > print "thread-", a, " ", task.inspect, "\n" > end > end > end > > 10.times do |t| > puts "t #{t}" > 30.times { |s| puts "s #{s}"; QUEUE.enq(s) } > threads.size.times { QUEUE.enq QUEUE } > threads.each {|t| t.join} > end > > In my loop "30 times" 5 * 6 threads are created. > But when I add "10 times" I should see 10 * what I had earlier. This is > not happening. I have my 5 * 6 threads created and that's all. Could you > explain why ? The first time around you insert 30 elements and then one end-of-work token (the QUEUE itself) per thread. Then you join the threads, so the main thread will wait at that point. The 5 threads will consume the 30 tokens and eventually consume the end-of-work token and exit. After that, the main thread goes through the next 9 iterations inserting items in the queue, but there are no more threads to consume the queue items. Jesus.
|
Pages: 1 Prev: ruby bounties--list of bounties Next: New gem snmpstats |