From: Dan Hinz Date: 2006-01-17T06:01:19+09:00 Subject: Re: There's a deadlock in code in the PickAxe, and I'm not seeing why: --=-CYFShNWVlLEIiPp+mdlO Content-Type: text/plain Content-Transfer-Encoding: 7bit Hummmm, no help here, works fine for me. Ruby 1.8.2, RH-8... pridith:[2006.01.16-15:54]:time ./threads.rb nil 1000000 32.200u 0.013x 0:32.59 99.1% 0+0k 0+0io 231pf+0w pridith:[2006.01.16-15:56]: -dwh- On Mon, 2006-01-16 at 15:33, Dave Thomas wrote: > The following code (form pg. 739) deadlocks with a large count. What > am I missing? > > Cheers > > > Dave > > > > > require 'thwait' > require 'sync' > > class Counter > attr_reader :total_count > def initialize > @total_count = 0 > @count_down = 0 > @sync = Sync.new > end > def inc > @sync.synchronize(:EX) do > @total_count += 1 > @count_down -= 1 > end > end > def test_consistent > @sync.synchronize(:SH) do > fail "Bad counts" unless @total_count + @count_down == 0 > end > end > end > > count = Counter.new > waiter = ThreadsWait.new([]) > > # create 10 threads that each inc() 10,000 times > 10.times do > waiter.join_nowait(Thread.new { 100_000.times do > count.inc > count.test_consistent > end }) > end > > p waiter.all_waits > p count.total_count > -- I not only live each endless day in grief, but live each day thinking about living each day in grief. -- C.S. Lewis Daniel W. Hinz Xerox Corp: XOG/SEBU/MCD/EIDC/ISM&D MS: 111-03J e-mail: dhinz@eng.mc.xerox.com 800 Phillips Road TEL: 585.422.8078 Webster, NY 14580 --=-CYFShNWVlLEIiPp+mdlO--