Threads

Never mind, I resolved the issue by reading a bit more. Sorry to waste the thread.

Cheers,
Phy

···

--- On Tue, 10/14/08, Phy Prabab <phyprabab@yahoo.com> wrote:

From: Phy Prabab <phyprabab@yahoo.com>
Subject: Threads
To: ruby-talk@ruby-lang.org
Date: Tuesday, October 14, 2008, 4:31 AM
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

Can you share with us?

Randy Kramer

···

On Tuesday 14 October 2008 04:48 am, Phy Prabab wrote:

Never mind, I resolved the issue by reading a bit more.

--
I didn't have time to write a short letter, so I created a video
instead.--with apologies to Cicero, et.al.

I think the reason is that there is nothing called thread.id and thats why
threads die as they start excution

···

On Tue, Oct 14, 2008 at 5:59 PM, Randy Kramer <rhkramer@gmail.com> wrote:

On Tuesday 14 October 2008 04:48 am, Phy Prabab wrote:
> Never mind, I resolved the issue by reading a bit more.

Can you share with us?

Randy Kramer
--
I didn't have time to write a short letter, so I created a video
instead.--with apologies to Cicero, et.al.

Now, this is strange reasoning. If we're guessing: the reason is more
likely that main thread exited without waiting for other threads.
See:

17:00:15 OPSC_Gold_bas_dev_R1.2.3$ ruby -e 'Thread.new { 10.times { p
1; sleep 10 } }; puts "done"'
1
done
17:09:33 OPSC_Gold_bas_dev_R1.2.3$

Kind regards

robert

···

2008/10/14 Piyush Ranjan <piyush.pr@gmail.com>:

I think the reason is that there is nothing called thread.id and thats why
threads die as they start excution

--
remember.guy do |as, often| as.you_can - without end

Hi guys,

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.5
    end
  }
end

Yes, there was a bug in my code, that being a typo of thread.id (should be Thread.id), but the biggest thing, was the fact that I needed to do a thread join. So I added this:

threads.each {|eachThread| eachThread.join}

Now on to signaling!

Cheers, Phy

···

--- On Tue, 10/14/08, Piyush Ranjan <piyush.pr@gmail.com> wrote:

From: Piyush Ranjan <piyush.pr@gmail.com>
Subject: Re: Threads
To: "ruby-talk ML" <ruby-talk@ruby-lang.org>
Date: Tuesday, October 14, 2008, 10:40 AM
I think the reason is that there is nothing called thread.id
and thats why
threads die as they start excution

On Tue, Oct 14, 2008 at 5:59 PM, Randy Kramer > <rhkramer@gmail.com> wrote:

> On Tuesday 14 October 2008 04:48 am, Phy Prabab wrote:
> > Never mind, I resolved the issue by reading a bit
more.
>
> Can you share with us?
>
> Randy Kramer
> --
> I didn't have time to write a short letter, so I
created a video
> instead.--with apologies to Cicero, et.al.
>
>

Phy Prabab wrote:

Yes, there was a bug in my code, that being a typo of thread.id (should
be Thread.id), but the biggest thing, was the fact that I needed to do a
thread join. So I added this:

threads.each {|eachThread| eachThread.join}

Now on to signaling!

I think your code still isn't quite what you want. Try

require 'thread'

threads =
mutex = Mutex.new
STDOUT.sync = true

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

for space in list
  threads << Thread.new(space) {|numericalSpace|
    while true
      mutex.synchronize do
        puts "#{Thread.current.object_id}: #{numericalSpace}"
      end
      sleep 2.5
    end
  }
end

threads.each {|eachThread| eachThread.join}

···

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