[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Threads and loops

Emil Sandin

8/16/2007 11:20:00 AM

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-....

2 Answers

Lionel Bouton

8/16/2007 11:24:00 AM

0

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.

Luis Parravicini

8/16/2007 11:57:00 AM

0

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?

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


--
Luis Parravicini
http://ktulu.co...