From: Abhijit Sarkar Date: 2013-02-25T00:51:17+09:00 Subject: Re: Ruby Multithreaded producer-consumer problem First of all, Sean's modified version works!. It does not run into the deadlock that mine does. That being said, I'll answer his questions below and, for newbies who are facing similar problem like mine, also point out the difference that made his code work and mine not. Sean O'halpin wrote in post #1098770: > - it's really not clear what you're trying to do - a problem > definition would help. When I wrote this program about 3 weeks back, I was just starting to learn Ruby and I picked on different aspects of the language that I thought I needed practice on. > - as Robert pointed out, using Thread.abort_on_exception = true would > help. You would have found that Math does not have a rand method for > example. Lesson learnt, didn't know. I guess this is one issue with interpreted languages that you don't see most problems until runtime. > - the only shared state you need to protect access to is the > @orange_count variable - it's unnecessary to use synchronize around > the whole loop. True. I thought then that @age also needs protection but on closer review now, it doesn't. > - you don't check whether the exit condition for the orange_picker > thread is true before waiting on the CV. This will halt your thread > indefinitely when the age_increaser thread exits before the > orange_picker thread starts the wait. THIS was causing the deadlock. When I added just the check to my program, it worked flawlessly. The missing check is the consequence of (wrongly) thinking that @age is also protected by the sync block. Thank you much for taking the time to troubleshoot the issue. I'd also attempt a Monitor version of the same program later but for now I am working on the Ruby Quiz challenges. -- Posted via http://www.ruby-forum.com/.