There's a deadlock in code in the PickAxe, and I'm not seeing why:

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.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
 
D

Dan Hinz

--=-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-

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: (e-mail address removed)
800 Phillips Road TEL: 585.422.8078
Webster, NY 14580

--=-CYFShNWVlLEIiPp+mdlO--
 
J

Joel VanderWerf

Dave said:
The following code (form pg. 739) deadlocks with a large count. What am
I missing?

Ok here, with ruby-1.8.4, Linux 2.6.12, Ubuntu. I tried 100 threads,
10_000 iterations, too.
 
D

Dave Thomas

works for me on mac and linux. what are you on?

OSX/ruby 1.8.2 (2004-12-30) [powerpc-darwin8.2.0]

dave[CVS/rubybook 19:52:46] ruby t.rb
deadlock 0x3a4e40: sleep:J(0x3b090c) - /Users/dave/ruby1.8/lib/ruby/
1.8/thwait.rb:121
deadlock 0x3b090c: sleep:- - /Users/dave/ruby1.8/lib/ruby/1.8/
sync.rb:150
...
deadlock 0x1b4af4: sleep:- - /Users/dave/ruby1.8/lib/ruby/1.8/
sync.rb:150
deadlock 0x1d5790: sleep:- (main) - /Users/dave/ruby1.8/lib/ruby/1.8/
thread.rb:281
/Users/dave/ruby1.8/lib/ruby/1.8/thwait.rb:119:in `pop': Thread
(0x3ca744): deadlock (fatal)
from /Users/dave/ruby1.8/lib/ruby/1.8/thwait.rb:139:in
`next_wait'
from /Users/dave/ruby1.8/lib/ruby/1.8/thwait.rb:154:in
`all_waits'
from t.rb:35


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

Anyone know if that would cause a problem?


Dave
 
D

Dave Thomas

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?


Dave
 
E

Eero Saynatkari

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?

ruerue@yawn$ ruby -v
ruby 1.8.4 (2005-12-24) [amd64-freebsd6]


E
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top