From: Abhijit Sarkar Date: 2013-02-14T21:32:44+09:00 Subject: Re: Ruby Multithreaded producer-consumer problem Carlo E. Prelz wrote in post #1096828: > Instance variables are only accessible from within methods. Lines > 83-86 are not within a method. Take lines 83-86 out of the class > definition, and change them to something like: > > worker=Worker.new(Mutex.new,ConditionVariable.new,OrangeTree.new) > worker.do_some_work > until(worker.orange_tree.age==OrangeTree::AGE_TO_DIE) > # wait for the Threads to finish > end > > Of course, variable orange_tree has to be visible from the outside, so > you have to add something like > > attr_reader(:orange_tree) > > at the beginning of the Worker class. This way you should not have > those exceptions anymore. After making the changes you suggested and adding joins inside class Worker, (file attached) I am now getting: Orange picker going to sleep for 5 Orange picker woke up after sleeping for 5 Sorry, no Oranges to pick Orange picker waiting patiently... Age increaser going to sleep for 2 Age increaser woke up after sleeping for 2 /Users/Abhijit/Workspace/eclipse/ruby/learn_to_program/orange_tree.rb:76:in `join': deadlock detected (fatal) from /Users/Abhijit/Workspace/eclipse/ruby/learn_to_program/orange_tree.rb:76:in `do_some_work' from /Users/Abhijit/Workspace/eclipse/ruby/learn_to_program/orange_tree.rb:82:in `' from /Users/Abhijit/Workspace/eclipse/ruby/learn_to_program/orange_tree.rb:39:in `
' Attachments: http://www.ruby-forum.com/attachment/8128/orange_tree.rb -- Posted via http://www.ruby-forum.com/.