# A question with

**URL:** <https://www.ruby-forum.com/t/a-question-with/174019>\
**Category:** Ruby\
**Created:** [August 19, 2009, 3:52pm UTC](https://www.ruby-forum.com/t/a-question-with/174019 "2009-08-19T15:52:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Im\_still](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/6de8d8/32.png) [@Im\_still](https://www.ruby-forum.com/u/Im_still)\
**Post date:** [August 19, 2009, 3:52pm UTC](https://www.ruby-forum.com/t/a-question-with/174019/1 "2009-08-19T15:52:29Z")

</div>

hi  
when im running a code like this

```
 a = 0
  Thread.start{
  while true
    puts "#{a+=1}"
  end
  }

```

This will be terminated automatically but wont keep on looping as  
expected.  
And by my test, the running result will always terminated while a =  
41.means  
after putting “41” it stops.

Im doing this in windows vista. I dont know if its OS related issue or  
not.  
or maybe im doing wrong ? anyone can tell me?

---

<div class="post-metadata">

**Author:** ![Im\_still](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/6de8d8/32.png) [@Im\_still](https://www.ruby-forum.com/u/Im_still)\
**Post date:** [August 19, 2009, 4:07pm UTC](https://www.ruby-forum.com/t/a-question-with/174019/2 "2009-08-19T16:07:10Z")

</div>

Im still wrote:

> hi  
> when im running a code like this
> 
> ```
> a = 0
> Thread.start{
> while true
> puts "#{a+=1}"
> end
> }
> 
> ```
> 
> This will be terminated automatically but wont keep on looping as  
> expected.

When a Ruby program terminates, all threads are killed, regardless of  
their states. (pickaxe2 p. 137)

> And by my test, the running result will always terminated while a =  
> 41.means  
> after putting “41” it stops.

It seems strange to me that the thread would get killed at the same  
point every time.

> Im doing this in windows vista. I dont know if its OS related issue or  
> not.  
> or maybe im doing wrong ? anyone can tell me?

…you can wait for a particular thread to finish by calling that  
thread’s Thread#join method. The calling thread [e.g. your main  
program] will block until the given thread is finished…If you don’t  
want to block forever, you can give join a timeout parameter…  
(pickaxe2 p. 137)

---

<div class="post-metadata">

**Author:** ![Im\_still](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/6de8d8/32.png) [@Im\_still](https://www.ruby-forum.com/u/Im_still)\
**Post date:** [August 19, 2009, 4:58pm UTC](https://www.ruby-forum.com/t/a-question-with/174019/3 "2009-08-19T16:58:03Z")

</div>

My guess is that since you’re not joining the thread to the main  
thread, it just terminates after the main thread exits. Try this:

a = 0  
thread = Thread.new do  
while true  
puts “#{a+=1}”  
end  
end  
thread.join

Alex

---

<div class="post-metadata">

**Author:** ![Im\_still](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/6de8d8/32.png) [@Im\_still](https://www.ruby-forum.com/u/Im_still)\
**Post date:** [August 19, 2009, 7:58pm UTC](https://www.ruby-forum.com/t/a-question-with/174019/4 "2009-08-19T19:58:58Z")

</div>

thanks Alex. I solved it that way. 😃

2009/8/19 Alex [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
