From: Sean O'Halpin Date: 2011-10-16T05:21:31+09:00 Subject: Re: deadlock producer consumer On Sat, Oct 15, 2011 at 3:26 PM, rubix Rubix wrote: > I tried simple Queue and it doesn't work too > because I think it a circular wait, the consumer consume from a queue > but produce new data and push them to the other queue > the producer make the same thing too > and i think that's why I have deadlock but I don't know how to solve the > problem Is this the kind of behaviour you're looking for? require 'thread' producer_queue = Queue.new consumer_queue = Queue.new def log(*a) STDERR.print a.inspect + "\n" end producer = Thread.start do loop do log :producer, :pop item = producer_queue.pop log :producer, :item, item item[:producer] += 1 log :producer, :push, item consumer_queue.push(item) end end consumer = Thread.start do loop do log :consumer, :pop item = consumer_queue.pop log :consumer, :item, item item[:consumer] += 1 log :consumer, :push, item producer_queue.push(item) end end # start things off producer_queue.push({ :producer => 0, :consumer => 0}) # either will work #consumer_queue.push({ :producer => 0, :consumer => 0}) consumer.join producer.join Regards, Sean