From: Ryan Davis Date: 2013-02-15T04:38:14+09:00 Subject: Re: Ruby Multithreaded producer-consumer problem That's great. That's the real problem the author wants you to solve. Sit down with some paper and crayons / colored pencils and draw a picture of the objects and what they have access to via your code. Then act out the process. Can you understand how the deadlock occurs? What can you do to avoid it? You might want to google dining philosophers for another example. On Feb 14, 2013, at 4:32, Abhijit Sarkar wrote: > 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/. >