Forum: Ruby There's a deadlock in code in the PickAxe, and I'm not seein

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
5d06917e13b29bcff1c1609492c06873?d=identicon&s=25 Dave Thomas (Guest)
on 2006-01-17 01:24
(Received via mailing list)
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
47b1910084592eb77a032bc7d8d1a84e?d=identicon&s=25 Joel VanderWerf (Guest)
on 2006-01-17 01:58
(Received via mailing list)
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.
Cb48ca5059faf7409a5ab3745a964696?d=identicon&s=25 unknown (Guest)
on 2006-01-21 15:40
(Received via mailing list)
On Tue, 17 Jan 2006, Dave Thomas 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?


-a
E997cf907c21c66a6ca2e1e78cd8ebea?d=identicon&s=25 Dan Hinz (Guest)
on 2006-01-21 15:40
(Received via mailing list)
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:

>
>    def inc
> 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
38a8230ed3d5c685558b4f0aad3fc74b?d=identicon&s=25 Joe Van Dyk (Guest)
on 2006-01-21 15:40
(Received via mailing list)
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...
>

Same for Ruby 1.8.3 on Ubuntu i486-linux.
5d06917e13b29bcff1c1609492c06873?d=identicon&s=25 Dave Thomas (Guest)
on 2006-01-21 15:40
(Received via mailing list)
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?

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
5d06917e13b29bcff1c1609492c06873?d=identicon&s=25 Dave Thomas (Guest)
on 2006-01-21 15:40
(Received via mailing list)
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?
>

Indeed, rebuilding my local Ruby with --enable-pthread fixes it. Is
this a bug in threading?


Dave
5c7bdd14d6885c8275eaf78be41d120a?d=identicon&s=25 Eero Saynatkari (Guest)
on 2006-01-21 15:40
(Received via mailing list)
On 2006.01.17 11:09, Dave Thomas wrote:
>
> 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
06c1bab0fb222c7426c02887cd728936?d=identicon&s=25 Johannes Friestad (Guest)
on 2006-01-21 15:44
(Received via mailing list)
> On Tue, 17 Jan 2006, Dave Thomas wrote:
>
 > The following code (form pg. 739) deadlocks with a large count. What
am I
 > missing?

Works on Ruby 1.8.2, WinXP.

jf
This topic is locked and can not be replied to.