Forum: Ruby Threads

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
555c1193071e97926fa67c038244c047?d=identicon&s=25 Phy Prabab (Guest)
on 2008-10-14 10:39
(Received via mailing list)
Hello,

So I am new to ruby and have a question about threads.  Yes, I
researched looking for an answer to my question without much luck.  I am
trying to create a bunch of threads that will run forever processing
numerical information.  I wrote a very simple example of what I thought
would work:

threads = []

list = %w(aS, bS, cS, dS, eS, fS, gS, hS)

for space in list
  threads << Thread.new(space) {|numericalSpace|
    while 1
      puts "#{thread.id}: #{numericalSpace}"
      sleep 2
    end
  }
end

This does not work as I thought it would, that is it would spin off a
bunch of threads and every two seconds I would get a thread print the
simple line to the terminal.

So, what am I missing?

Thanks in advance!
Phy
Bec38d63650c8912b6ba9b557fb953b9?d=identicon&s=25 Roger Pack (rogerdpack)
on 2008-10-17 08:12
> This does not work as I thought it would, that is it would spin off a
> bunch of threads and every two seconds I would get a thread print the
> simple line to the terminal.
>
> So, what am I missing?

Try adding a Thread.abort_on_exception = true to the top.
Also note that if the "main" thread ever terminates then it kills all
other threads and ends the program
Cheers.
-=R
This topic is locked and can not be replied to.