Forum: Ruby Re: 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:48
(Received via mailing list)
Never mind, I resolved the issue by reading a bit more.  Sorry to waste
the thread.

Cheers,
Phy
F5b3c1ebfb2e9fc5f67bb48b119f6054?d=identicon&s=25 Randy Kramer (Guest)
on 2008-10-14 14:30
(Received via mailing list)
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
7e85c8b066ffb802cba005e90c6c616e?d=identicon&s=25 Piyush Ranjan (Guest)
on 2008-10-14 16:42
(Received via mailing list)
I think the reason is that there is nothing called thread.id and thats
why
threads die as they start excution
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2008-10-14 17:11
(Received via mailing list)
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

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
555c1193071e97926fa67c038244c047?d=identicon&s=25 Phy Prabab (Guest)
on 2008-10-14 19:26
(Received via mailing list)
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
Beb77c4602c3cac7a12149431366ed11?d=identicon&s=25 The Higgs bozo (higgsbozo)
on 2008-10-14 19:58
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}
This topic is locked and can not be replied to.