D
Dave Thomas
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.synchronizeEX) do
@total_count += 1
@count_down -= 1
end
end
def test_consistent
@sync.synchronizeSH) 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
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.synchronizeEX) do
@total_count += 1
@count_down -= 1
end
end
def test_consistent
@sync.synchronizeSH) 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