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
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
The following code (form pg. 739) deadlocks with a large count. What am I missing?
--
strong and healthy, who thinks of sickness until it strikes like lightning?
preoccupied with the world, who thinks of death, until it arrives like
thunder? -- milarepa
It doesn't always do it, but with 100,000 iterations, it does it most of the time.
Interestingly, it doesn't seem to do it with the precompiled Ruby. I'm wondering if I build my local version with the wrong threading options: the Apple build has --enable-pthread, and mine doesn't
Indeed, rebuilding my local Ruby with --enable-pthread fixes it. Is this a bug in threading?
Dave
···
On Jan 16, 2006, at 8:02 PM, Dave Thomas wrote:
Interestingly, it doesn't seem to do it with the precompiled Ruby. I'm wondering if I build my local version with the wrong threading options: the Apple build has --enable-pthread, and mine doesn't
>
>Interestingly, it doesn't seem to do it with the precompiled Ruby.
>I'm wondering if I build my local version with the wrong threading
>options: the Apple build has --enable-pthread, and mine doesn't
>
>Anyone know if that would cause a problem?
>
Indeed, rebuilding my local Ruby with --enable-pthread fixes it. Is
this a bug in threading?
It seems to work OK here both ways. OSX threading, perhaps?