From: Dave Thomas Date: 2006-01-17T05:33:38+09:00 Subject: There's a deadlock in code in the PickAxe, and I'm not seeing why: 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