# There's a deadlock in code in the PickAxe, and I'm not seein

**URL:** <https://www.ruby-forum.com/t/theres-a-deadlock-in-code-in-the-pickaxe-and-im-not-seein/52601>\
**Category:** Ruby\
**Created:** [January 17, 2006, 1:24am UTC](https://www.ruby-forum.com/t/theres-a-deadlock-in-code-in-the-pickaxe-and-im-not-seein/52601 "2006-01-17T01:24:41Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![dirtytree](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@dirtytree](https://www.ruby-forum.com/u/dirtytree)\
**Post date:** [January 17, 2006, 1:24am UTC](https://www.ruby-forum.com/t/theres-a-deadlock-in-code-in-the-pickaxe-and-im-not-seein/52601/1 "2006-01-17T01:24:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dirtytree](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@dirtytree](https://www.ruby-forum.com/u/dirtytree)\
**Post date:** [January 17, 2006, 1:58am UTC](https://www.ruby-forum.com/t/theres-a-deadlock-in-code-in-the-pickaxe-and-im-not-seein/52601/2 "2006-01-17T01:58:05Z")

</div>

Dave T. 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.

---

<div class="post-metadata">

**Author:** ![dirtytree](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@dirtytree](https://www.ruby-forum.com/u/dirtytree)\
**Post date:** [January 21, 2006, 3:40pm UTC](https://www.ruby-forum.com/t/theres-a-deadlock-in-code-in-the-pickaxe-and-im-not-seein/52601/3 "2006-01-21T15:40:27Z")

</div>

On Tue, 17 Jan 2006, Dave T. 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

---

<div class="post-metadata">

**Author:** ![dirtytree](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@dirtytree](https://www.ruby-forum.com/u/dirtytree)\
**Post date:** [January 21, 2006, 3:40pm UTC](https://www.ruby-forum.com/t/theres-a-deadlock-in-code-in-the-pickaxe-and-im-not-seein/52601/4 "2006-01-21T15:40:37Z")

</div>

On 1/16/06, Dan H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![dirtytree](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@dirtytree](https://www.ruby-forum.com/u/dirtytree)\
**Post date:** [January 21, 2006, 3:40pm UTC](https://www.ruby-forum.com/t/theres-a-deadlock-in-code-in-the-pickaxe-and-im-not-seein/52601/5 "2006-01-21T15:40:38Z")

</div>

On Jan 16, 2006, at 4:41 PM, removed\_email\_address@domain.invalid 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

---

<div class="post-metadata">

**Author:** ![dirtytree](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@dirtytree](https://www.ruby-forum.com/u/dirtytree)\
**Post date:** [January 21, 2006, 3:40pm UTC](https://www.ruby-forum.com/t/theres-a-deadlock-in-code-in-the-pickaxe-and-im-not-seein/52601/6 "2006-01-21T15:40:30Z")

</div>

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 T. 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: removed\_email\_address@domain.invalid  
800 Phillips Road TEL: 585.422.8078  
Webster, NY 14580

---

<div class="post-metadata">

**Author:** ![dirtytree](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@dirtytree](https://www.ruby-forum.com/u/dirtytree)\
**Post date:** [January 21, 2006, 3:40pm UTC](https://www.ruby-forum.com/t/theres-a-deadlock-in-code-in-the-pickaxe-and-im-not-seein/52601/7 "2006-01-21T15:40:39Z")

</div>

On 2006.01.17 11:09, Dave T. 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

---

<div class="post-metadata">

**Author:** ![dirtytree](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@dirtytree](https://www.ruby-forum.com/u/dirtytree)\
**Post date:** [January 21, 2006, 3:40pm UTC](https://www.ruby-forum.com/t/theres-a-deadlock-in-code-in-the-pickaxe-and-im-not-seein/52601/8 "2006-01-21T15:40:38Z")

</div>

On Jan 16, 2006, at 8:02 PM, Dave T. 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

---

<div class="post-metadata">

**Author:** ![dirtytree](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@dirtytree](https://www.ruby-forum.com/u/dirtytree)\
**Post date:** [January 21, 2006, 3:44pm UTC](https://www.ruby-forum.com/t/theres-a-deadlock-in-code-in-the-pickaxe-and-im-not-seein/52601/9 "2006-01-21T15:44:28Z")

</div>

> On Tue, 17 Jan 2006, Dave T. wrote:
> 
> The following code (form pg. 739) deadlocks with a large count. What  
> am I  
> missing?

Works on Ruby 1.8.2, WinXP.

jf
