Threads and loops

Hi, if I run this script:

  loop{
    puts "hello"
    sleep 1
  }

It outputs "hello" every second.
But if I place it in a separate thread:

Thread.new do
  loop{
    puts "hello"
    sleep 1
  }
end

it prints it only one time and then exits.
Why is that?

···

--
Posted via http://www.ruby-forum.com/.

Emil Sandin wrote the following on 16.08.2007 13:20 :

[...]
Thread.new do
  loop{
    puts "hello"
    sleep 1
  }
end

it prints it only one time and then exits.
Why is that?
  
You don't wait for the Thread to stop to exit your program.

When your program exits, the thread dies. Take a look at Thread.join
http://www.ruby-doc.org/core/classes/Thread.html#M000474

···

On 8/16/07, Emil Sandin <esandin@gmail.com> wrote:

Hi, if I run this script:

  loop{
    puts "hello"
    sleep 1
  }

It outputs "hello" every second.
But if I place it in a separate thread:

Thread.new do
  loop{
    puts "hello"
    sleep 1
  }
end

it prints it only one time and then exits.
Why is that?

--
Luis Parravicini
http://ktulu.com.ar/blog/