On Sun, Feb 24, 2013 at 4:51 PM, Abhijit Sarkar <[email protected]> wrote:

> Sean O'halpin wrote in post #1098770:

>> - 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.

Access to at least these variables needs proper synchronization:
Worker@next_run
OrangeTree@age
OrangeTree@orange_count

>> - 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.

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.

> THIS was causing the deadlock.

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.

> 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.

Can you please share the code which is working and the change you
believe made the difference?

Note, I was still using
http://www.ruby-forum.com/attachment/8132/orange_tree.rb as basis.

Kind regards

robert


-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

-- 
[email protected] | 
https://groups.google.com/d/forum/ruby-talk-google?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"ruby-talk-google" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to