# Long loops inside threads

**URL:** <https://www.ruby-forum.com/t/long-loops-inside-threads/210037>\
**Category:** Ruby\
**Created:** [July 28, 2011, 10:12pm UTC](https://www.ruby-forum.com/t/long-loops-inside-threads/210037 "2011-07-28T22:12:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [July 28, 2011, 10:12pm UTC](https://www.ruby-forum.com/t/long-loops-inside-threads/210037/1 "2011-07-28T22:12:20Z")

</div>

Hi,

I have a newbie question 🙂

If I run the following code outside a thread:

count=0  
while count \< 10000 do  
puts “x”  
count +=1  
end

I get 10000 ‘x’.

But, if I run the same code inside a thread, I receive different number  
of ‘x’ on each execution:

7932  
4305  
4431

What I’m doing wrong?

I need to execute the same command inside a thread 10000 times! 🙂

Thank you.

---

<div class="post-metadata">

**Author:** ![Miguel\_Angel\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/74df32/32.png) [@Miguel\_Angel\_N](https://www.ruby-forum.com/u/Miguel_Angel_N)\
**Post date:** [July 28, 2011, 10:32pm UTC](https://www.ruby-forum.com/t/long-loops-inside-threads/210037/2 "2011-07-28T22:32:36Z")

</div>

Are you join’ing the thread? If not your main thread is likely to exit  
before the counting thread has.

> cat /tmp/test.rb  
> require ‘thread’

t = Thread.new {  
count=0  
while count \< 10000 do  
puts “x”  
count +=1  
end  
}  
t.join

> ruby /tmp/test.rb |wc -l  
> 10000

> ruby /tmp/test.rb |wc -l  
> 10000

> ruby /tmp/test.rb |wc -l  
> 10000

Sam

---

<div class="post-metadata">

**Author:** ![Miguel\_Angel\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/74df32/32.png) [@Miguel\_Angel\_N](https://www.ruby-forum.com/u/Miguel_Angel_N)\
**Post date:** [July 28, 2011, 10:39pm UTC](https://www.ruby-forum.com/t/long-loops-inside-threads/210037/3 "2011-07-28T22:39:32Z")

</div>

Hi,

That was the problem! I have to reread the threat chapter of the ruby  
book 😉

Thank you.

Sam D. wrote in post #1013619:

> Are you join’ing the thread? If not your main thread is likely to exit  
> before the counting thread has.
> 
> > cat /tmp/test.rb  
> > require ‘thread’
> 
> t = Thread.new {  
> count=0  
> while count \< 10000 do  
> puts “x”  
> count +=1  
> end  
> }  
> t.join
> 
> > ruby /tmp/test.rb |wc -l  
> > 10000
> 
> > ruby /tmp/test.rb |wc -l  
> > 10000
> 
> > ruby /tmp/test.rb |wc -l  
> > 10000
> 
> Sam

---

<div class="post-metadata">

**Author:** ![Miguel\_Angel\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/74df32/32.png) [@Miguel\_Angel\_N](https://www.ruby-forum.com/u/Miguel_Angel_N)\
**Post date:** [July 28, 2011, 10:48pm UTC](https://www.ruby-forum.com/t/long-loops-inside-threads/210037/4 "2011-07-28T22:48:42Z")

</div>

No worries. You are on the doorstep of an exciting and endlessly  
frustrating world. Have fun =]

Sam

---

<div class="post-metadata">

**Author:** ![Miguel\_Angel\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/74df32/32.png) [@Miguel\_Angel\_N](https://www.ruby-forum.com/u/Miguel_Angel_N)\
**Post date:** [July 29, 2011, 1:11pm UTC](https://www.ruby-forum.com/t/long-loops-inside-threads/210037/5 "2011-07-29T13:11:24Z")

</div>

Miguel Angel Nieto wrote in post #1013621:

> That was the problem! I have to reread the threat chapter of the ruby  
> book 😉

You can also use Thread#value which does an implicit join like this:

irb(main):003:0\> t = Thread.new do  
irb(main):004:1\* count = 0  
irb(main):005:1\> while count \< 1000  
irb(main):006:2\> count += 1  
irb(main):007:2\> end  
irb(main):008:1\> count  
irb(main):009:1\> end; t.value  
=\> 1000

Kind regards

robert
