# Thwait

**URL:** <https://www.ruby-forum.com/t/thwait/65879>\
**Category:** Ruby\
**Created:** [August 22, 2006, 1:50am UTC](https://www.ruby-forum.com/t/thwait/65879 "2006-08-22T01:50:48Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 22, 2006, 1:50am UTC](https://www.ruby-forum.com/t/thwait/65879/1 "2006-08-22T01:50:48Z")

</div>

i’m unsure if this should be expected to cause deadlock or not?

ruby -r thwait -e’ ThWait.all\_waits(\*Array.new(42){  
Thread.new{Thread.stop} }) ’

for 1.8.1 - 1.8.4 it does.

thoughts?

-a

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 22, 2006, 1:56am UTC](https://www.ruby-forum.com/t/thwait/65879/2 "2006-08-22T01:56:49Z")

</div>

Ara.T.Howard wrote:

> -a  
> Seems like it would. You create a bunch of threads, tell them not to  
> run, then tell the main thread to wait on all the stopped threads.

There’s no way for this program to complete or continue.

-Justin

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 22, 2006, 2:10am UTC](https://www.ruby-forum.com/t/thwait/65879/3 "2006-08-22T02:10:49Z")

</div>

On Tue, 22 Aug 2006, Justin C. wrote:

> > -a
> 
> Seems like it would. You create a bunch of threads, tell them not to run,  
> then tell the main thread to wait on all the stopped threads.
> 
> There’s no way for this program to complete or continue.

it seems like it should just hang then. otherwise this program wouldn’t  
be  
able to work

harp:~ \> cat a.rb  
require ‘thwait’

loop{  
stopped = Thread.new{ Thread.stop }

```
 notify = Thread.new{ stopped.wakeup }

 ThWait.all_waits stopped

```

}

this code **should** be able to run - but it won’t. the docs say:

harp:~ \> ri ThreadsWait  
----------------------------------------------------- Class:  
ThreadsWait  
This class watches for termination of multiple threads. Basic  
functionality (wait until specified threads have terminated) can  
be  
accessed through the class method ThreadsWait::all\_waits. Finer  
control can be gained using instance methods.

a sleeping thread has not terminated, but whose to say it won’t at some  
point  
in the future?

for instance. this is valid ruby

ruby -e’ Thread.new{ sleep }.join ’

it just hangs forever. similarly i feel that

ThWait.all\_waits Thread.new{ sleep }

should also hang - not deadlock.

regards.

-a

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 22, 2006, 2:17am UTC](https://www.ruby-forum.com/t/thwait/65879/4 "2006-08-22T02:17:18Z")

</div>

Ara.T.Howard wrote:

> i’m unsure if this should be expected to cause deadlock or not?
> 
> ruby -r thwait -e’ ThWait.all\_waits(\*Array.new(42){  
> Thread.new{Thread.stop} }) ’
> 
> for 1.8.1 - 1.8.4 it does.
> 
> thoughts?

Ruby has a pretty happy-go-lucky thread introspection  
methodology although it seems to always be correct. It  
can detect potential deadlocks pretty easily and will  
just bow out. Ran into it just using plain Threads some  
time back.

> -a

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 22, 2006, 2:46am UTC](https://www.ruby-forum.com/t/thwait/65879/5 "2006-08-22T02:46:52Z")

</div>

removed\_email\_address@domain.invalid wrote:

> > >
> 
> wouldn’t be  
> ThWait.all\_waits stopped  
> }
> 
> this code **should** be able to run - but it won’t. the docs say:

It _does_ run. Try putting in an output statement in the loop. However,  
what happens (I’m guessing) is something like this:

stopped = Thread.new { #=\> context change

notify = Thread.new { stopped.wakeup } #=\> okay, stopped can run

# 

# Now, Thread.stop runs for stopped

# 

ThWait.all\_waits stopped #=\> now it will never be able to complete

> a sleeping thread has not terminated, but whose to say it won’t at  
> some point  
> in the future?

In some instances, like these examples, you can say that it won’t  
terminate. (I’m not sure if Ruby also detects deadlocks in more complex  
examples incorrectly.)

> for instance. this is valid ruby
> 
> ruby -e’ Thread.new{ sleep }.join ’
> 
> it just hangs forever. similarly i feel that
> 
> ThWait.all\_waits Thread.new{ sleep }
> 
> should also hang - not deadlock.

Well, I think what’s happening is that Ruby can detect that, in the  
second instance, there is no way for it not to deadlock. How could it  
not? I’m not sure about the first instance, though.

-Justin

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 22, 2006, 2:54am UTC](https://www.ruby-forum.com/t/thwait/65879/6 "2006-08-22T02:54:52Z")

</div>

Justin C. wrote:

> In some instances, like these examples, you can say that it won’t  
> terminate. (I’m not sure if Ruby also detects deadlocks in more  
> complex examples incorrectly.)

Uh, I didn’t mean Ruby is detecting these incorrectly - it is correct.  
But there may be more complex situations in which it Ruby says  
“Deadlock!” without there actually being a deadlock, I don’t know.

-Justin
