From: Justin Collins Date: 2011-10-15T10:51:46+09:00 Subject: Re: deadlock producer consumer On 10/14/2011 05:42 PM, rubix Rubix wrote: > Hi > I make the following test > > #-------------------------------------------------- > ProducerQueue = Containers::PriorityQueue.new > ConsumerQueue = Containers::Queue.new > > mutex = Mutex.new > access = ConditionVariable.new > > > initialize ProducerQueue > > producer = Thread.new do > loop do > mutex.synchronize { > while ProducerQueue.empty? do access.wait(mutex) end > > ProducerQueue.pop > somework > ConsumerQueue.push(result) > access.signal > } > end > end > consumer = Thread.new do > loop do > mutex.synchronize { > while ConsumerQueue.empty? do access.wait(mutex) end > > page = ConsumerQueue.pop() > somework > ProducerQueue.push(result) > access.signal > > } > end > end > > > consumer.join > producer.join > #---------------------------------------------------------- > > I keep having deadlock when executing it > What is wrong with the code? I think may be I have to add an other > ConditionVariable, the problem is the producer and the consumer use the > two queues > > Any Ideas > When you use mutex.synchronize, it's going to lock to mutex. When you call access.wait(), it releases that same mutex and waits for the mutex to be unlocked. But you have already locked it, and it should/can not unlock until after the block is finished. I'm not even sure what the behavior is here, but it's unlikely to work as you might expect. -Justin