From: Abhijit Sarkar Date: 2013-02-25T05:50:49+09:00 Subject: Re: Ruby Multithreaded producer-consumer problem Robert Klemme wrote in post #1098804: > Access to at least these variables needs proper synchronization: > Worker@next_run > OrangeTree@age > OrangeTree@orange_count It seems that you are looking at the Monitor version of the code. I was trying everything under the sun to get the code working so there exist a couple of versions. The version Sean fixed does NOT use a Monitor, it uses a Mutex. I've uploaded my Mutex and Monitor versions and Sean's Mutex version to my GitHub (https://github.com/abhijitsarkar/ruby/tree/master/learn_to_program). The names are self-descriptive (orange_tree_*) The Monitor version still has the deadlock issue but I have pushed it off for later. > No. If the age_increaser exits before the orange_picker starts to > wait it means that it will have set @next_run to :orange. > orange_picker won't block then. Again, guess we are talking about 2 different versions. > No, the deadlock error was caused by the fact that one of the two > threads died silently because of Math.rand(). When you fix that by > deleting "Math." your code works. > You are right that Math.rand was a culprit but it wasn't alone. In Mutex version, the deadlock happens without the following check even with Math.rand fixed. if @orange_tree.age < OrangeTree::AGE_TO_DIE @cv.wait(@mutex) end > Can you please share the code which is working and the change you > believe made the difference? https://github.com/abhijitsarkar/ruby/tree/master/learn_to_program/orange_tree_* I apologize if there was any confusion about different versions of the program. It is not so confusing in the forum posts but I guess may not be in the mailing list. -- Posted via http://www.ruby-forum.com/.