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

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

Dave Thomas wrote:

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.

···

--
      vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

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

-a

···

On Tue, 17 Jan 2006, Dave Thomas wrote:

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

Same for Ruby 1.8.3 on Ubuntu i486-linux.

···

On 1/16/06, Dan Hinz <dhinz@eng.mc.xerox.com> wrote:

Hummmm, no help here, works fine for me.

Ruby 1.8.2, RH-8...

Works on Ruby 1.8.2, WinXP.

jf

···

On Tue, 17 Jan 2006, Dave Thomas wrote:

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

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

···

On Jan 16, 2006, at 4:41 PM, ara.t.howard@noaa.gov wrote:

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

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

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

Anyone know if that would cause a problem?

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

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

Dave

E

···

On 2006.01.17 11:09, Dave Thomas wrote:

On Jan 16, 2006, at 8:02 PM, Dave Thomas wrote: