From: Markus Date: 2004-10-16T02:59:38+09:00 Subject: Re: Again some problem with my multithreaded teadrinker app Odd. I'll look at it some more this evening (though as I'm leaving for a week in Costa Rica this Sunday my time is growing short). Does it always deadlock on in same state (0 & 1 on the table)? Or is there any other pattern you can discern? -- Markus P.S. Your understanding of the fix is spot on. On Fri, 2004-10-15 at 10:49, Tassilo Horn wrote: > Markus writes: > > > I'd suggested this before (as the solution to your first problem) but it > > seemed to have gotten missed--the nesting of your looping/locking needs > > to be swapped. In the present case, reversing both of the " > > mutex.synchronize"/"while true" pairs (and their associated "}"/"end" > > pairs) should fix the problem. > > It seems to work if I start many threads, but if I only use three I get > an deadlock after one or two drinkers had their tea: > > (%:~/tmp/teadrinkers--mainline--0.3--patch-1)- ./TeaRoom.rb 3 > Starting Drinker0 > Starting Drinker1 > Starting Drinker2 > WAITER: Looking at the table! > WAITER: Oh, nothing on the table! > WAITER: Putting 2 and 1 on the table. > WAITER: Waiting for new appointments... > Drinker0: Tea and Water on table. I'll cook my tea! > Drinker0: Drinking my tea. I call the waiter again. > Drinker0: Now I'll read my newspaper. > WAITER: Looking at the table! > WAITER: Oh, nothing on the table! > WAITER: Putting 0 and 1 on the table. > WAITER: Waiting for new appointments... > Drinker2: Cup and Water on table. I'll cook my tea! > Drinker2: Drinking my tea. I call the waiter again. > Drinker2: Now I'll read my newspaper. > WAITER: Looking at the table! > WAITER: Oh, nothing on the table! > WAITER: Putting 1 and 0 on the table. > WAITER: Waiting for new appointments... > deadlock 0x4022b0dc: sleep:- - /usr/lib/ruby/1.8/thread.rb:195 > deadlock 0x40240798: sleep:J(0x4022acb8) (main) - ./TeaRoom.rb:81 > deadlock 0x4022acb8: sleep:- - /usr/lib/ruby/1.8/thread.rb:195 > deadlock 0x4022ade4: sleep:- - /usr/lib/ruby/1.8/thread.rb:195 > deadlock 0x4022af60: sleep:- - /usr/lib/ruby/1.8/thread.rb:195 > /usr/lib/ruby/1.8/thread.rb:195: Thread(0x4022af60): deadlock (fatal) > > > P.S. If the reason isn't clear, ask yourself when/under what condition > > (in the code as posted) did it ever leave the synchronize block? > > Never. And when the waiter calls drinker_cv.broadcast() always the > thread who entered the mutex.synchronize-block first and obtained the > lock first is woken up. Right? > > Thanks for your help, > Tassilo