# Thread example

**URL:** <https://www.ruby-forum.com/t/thread-example/182058>\
**Category:** Ruby\
**Created:** [January 22, 2010, 4:53am UTC](https://www.ruby-forum.com/t/thread-example/182058 "2010-01-22T04:53:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rubynut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e8cbc/32.png) [@rubynut](https://www.ruby-forum.com/u/rubynut)\
**Post date:** [January 22, 2010, 4:53am UTC](https://www.ruby-forum.com/t/thread-example/182058/1 "2010-01-22T04:53:31Z")

</div>

Hello,

Just copied the code piece from the ruby cookbook:

require ‘thread’  
class CounterThread \< Thread  
def initialize  
@count = 0  
@continue = true  
super do  
@count += 1 while @continue  
puts “I counted up to #{@count} before I was cruelly stopped.”  
end  
end

def stop  
@continue = false  
end  
end

counter = CounterThread.new  
sleep 2  
counter.stop

After I run it, I didn’s see any output, even the expected puts:  
“I counted up to #{@count} before I was cruelly stopped.”

Where is wrong?  
Thanks.

---

<div class="post-metadata">

**Author:** ![rubynut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e8cbc/32.png) [@rubynut](https://www.ruby-forum.com/u/rubynut)\
**Post date:** [January 22, 2010, 9:15am UTC](https://www.ruby-forum.com/t/thread-example/182058/2 "2010-01-22T09:15:08Z")

</div>

On Thu, Jan 21, 2010 at 10:53 PM, Ruby N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> After I run it, I didn’s see any output, even the expected puts:  
> “I counted up to #{@count} before I was cruelly stopped.”
> 
> Where is wrong?  
> Thanks.

The output is getting buffered so it’s just not getting displayed before  
the  
thread stops. To fix this, put “$stdout.flush” at the end of your code.

---

<div class="post-metadata">

**Author:** ![rubynut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e8cbc/32.png) [@rubynut](https://www.ruby-forum.com/u/rubynut)\
**Post date:** [January 22, 2010, 9:20am UTC](https://www.ruby-forum.com/t/thread-example/182058/3 "2010-01-22T09:20:59Z")

</div>

2010/1/22 Jacob M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> thread stops. To fix this, put “$stdout.flush” at the end of your code.  
> I would rather add “counter.join” at the end because otherwise process  
> may exit before the thread had time to generate the output. In that  
> case sync = true does not help much.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![rubynut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e8cbc/32.png) [@rubynut](https://www.ruby-forum.com/u/rubynut)\
**Post date:** [January 22, 2010, 9:49am UTC](https://www.ruby-forum.com/t/thread-example/182058/4 "2010-01-22T09:49:40Z")

</div>

On Fri, Jan 22, 2010 at 4:20 PM, Robert K.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I would rather add “counter.join” at the end because otherwise process  
> may exit before the thread had time to generate the output. Â In that  
> case sync = true does not help much.

That’s right.  
we need a thread\_object.join at the end.  
Thanks robert.
