# Re: Threads

**URL:** <https://www.ruby-forum.com/t/re-threads/150862>\
**Category:** Ruby\
**Created:** [October 14, 2008, 10:48am UTC](https://www.ruby-forum.com/t/re-threads/150862 "2008-10-14T10:48:47Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Phy\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a587f6/32.png) [@Phy\_P](https://www.ruby-forum.com/u/Phy_P)\
**Post date:** [October 14, 2008, 10:48am UTC](https://www.ruby-forum.com/t/re-threads/150862/1 "2008-10-14T10:48:47Z")

</div>

Never mind, I resolved the issue by reading a bit more. Sorry to waste  
the thread.

Cheers,  
Phy

---

<div class="post-metadata">

**Author:** ![Phy\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a587f6/32.png) [@Phy\_P](https://www.ruby-forum.com/u/Phy_P)\
**Post date:** [October 14, 2008, 2:30pm UTC](https://www.ruby-forum.com/t/re-threads/150862/2 "2008-10-14T14:30:05Z")

</div>

On Tuesday 14 October 2008 04:48 am, Phy P. wrote:

> Never mind, I resolved the issue by reading a bit more.

Can you share with us?

Randy K.

---

<div class="post-metadata">

**Author:** ![Phy\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a587f6/32.png) [@Phy\_P](https://www.ruby-forum.com/u/Phy_P)\
**Post date:** [October 14, 2008, 4:42pm UTC](https://www.ruby-forum.com/t/re-threads/150862/3 "2008-10-14T16:42:20Z")

</div>

I think the reason is that there is nothing called thread.id and thats  
why  
threads die as they start excution

---

<div class="post-metadata">

**Author:** ![Phy\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a587f6/32.png) [@Phy\_P](https://www.ruby-forum.com/u/Phy_P)\
**Post date:** [October 14, 2008, 5:11pm UTC](https://www.ruby-forum.com/t/re-threads/150862/4 "2008-10-14T17:11:09Z")

</div>

2008/10/14 Piyush R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> I think the reason is that there is nothing called thread.id and thats why  
> threads die as they start excution

Now, this is strange reasoning. If we’re guessing: the reason is more  
likely that main thread exited without waiting for other threads.  
See:

17:00:15 OPSC\_Gold\_bas\_dev\_R1.2.3$ ruby -e ‘Thread.new { 10.times { p  
1; sleep 10 } }; puts “done”’  
1  
done  
17:09:33 OPSC\_Gold\_bas\_dev\_R1.2.3$

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![Phy\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a587f6/32.png) [@Phy\_P](https://www.ruby-forum.com/u/Phy_P)\
**Post date:** [October 14, 2008, 7:58pm UTC](https://www.ruby-forum.com/t/re-threads/150862/5 "2008-10-14T19:58:34Z")

</div>

Phy P. wrote:

> Yes, there was a bug in my code, that being a typo of thread.id (should  
> be Thread.id), but the biggest thing, was the fact that I needed to do a  
> thread join. So I added this:
> 
> threads.each {|eachThread| eachThread.join}
> 
> Now on to signaling!

I think your code still isn’t quite what you want. Try

require ‘thread’

threads = []  
mutex = Mutex.new  
STDOUT.sync = true

list = %w(aS, bS, cS, dS, eS, fS, gS, hS)

for space in list  
threads \<\< Thread.new(space) {|numericalSpace|  
while true  
mutex.synchronize do  
puts “#{Thread.current.object\_id}: #{numericalSpace}”  
end  
sleep 2.5  
end  
}  
end

threads.each {|eachThread| eachThread.join}

---

<div class="post-metadata">

**Author:** ![Phy\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a587f6/32.png) [@Phy\_P](https://www.ruby-forum.com/u/Phy_P)\
**Post date:** [October 14, 2008, 7:26pm UTC](https://www.ruby-forum.com/t/re-threads/150862/6 "2008-10-14T19:26:38Z")

</div>

Hi guys,

threads = []

list = %w(aS, bS, cS, dS, eS, fS, gS, hS)

for space in list  
threads \<\< Thread.new(space) {|numericalSpace|  
while 1  
puts “#{Thread.id}: #{numericalSpace}”  
sleep 2.5  
end  
}  
end

Yes, there was a bug in my code, that being a typo of thread.id (should  
be Thread.id), but the biggest thing, was the fact that I needed to do a  
thread join. So I added this:

threads.each {|eachThread| eachThread.join}

Now on to signaling!

Cheers, Phy
