Re: 1.9.x Thread problem




If I run this using either ruby 1.9.1p0 (2009-01-30 revision 21907)
[i686-linux] or ruby 1.9.2dev (2009-03-05 trunk 22769) [i686-linux]
I get results I would not expect:
Joining thread , n=


It hasn't been defined which thread, the created child thread
or the creating parent thread, runs first at thread creation.
In 1.8, the child runs first by chance.



I have read and re-read this about 10 times now and this comment makes
it appear that the contract for Thread.new {block}, where the block is
run exclusively in the newly created thread, is violated.
Can you then explain why it runs consistently like this or a way in
which I can guarantee I am running in the first created child thread?
I would have expected to be inside of the child thread because the
code setting up the :index is inside the block passed to
Thread.new(). And I only put the created child Thread instances in
the Queue, so I should _only_ get child Thread instances when I call
Queue#pop.
require 'thread'
THREADS = 10

parent_id = Thread.current.object_id

queue = Queue.new
THREADS.times do |i|
queue << Thread.new do
th = Thread.current
th[:index] = i
raise "Running inside parent" if th.object_id == parent_id
sleep(2)
end
end

THREADS.times do
th = queue.pop
raise "Running inside parent" if th.object_id == parent_id
puts "Joining thread #{th[:index]}"
th.join()
end

I have simplified and changed things. I am quite certain that the
parent thread is not being joined or indeed added to the queue as no
exceptions are raised (unless this is a bad test) and I still get the
same (strange) output. Any other ideas?


=======================================================================
This email, including any attachments, is only for the intended
addressee. It is subject to copyright, is confidential and may be
the subject of legal or other privilege, none of which is waived or
lost by reason of this transmission.
If the receiver is not the intended addressee, please accept our
apologies, notify us by return, delete all copies and perform no
other act on the email.
Unfortunately, we cannot warrant that the email has not been
altered or corrupted during transmission.
=======================================================================


.



Relevant Pages

  • Threading in .Net...
    ... Then alert Child Thread 2 that there is a message in the queue. ... around the global memory structure that would prevent more than one ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Threading in .Net...
    ... The parent thread will spawn at least two child threads. ... Then alert Child Thread 2 that there is a message in the queue. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Thread question
    ... entered something into the queue? ... >> I have a main worker thread that spawns a child thread to do some listening. ... >> the parent thread, and have the parent thread respond to those events. ... >> Is it possible to have one thread raise events in another thread? ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Question about the example in perlthrtut
    ... So the first thread will not complete untill the last thread ... This first thread has a full queue and the joinblocks ... First thread creates a new second child thread and passes the prime 3 and an empty queue ... I don't know how threads are implemented in Perl too well. ...
    (comp.lang.perl.misc)
  • Re: Threading in .Net...
    ... The parent thread and the worker thread. ... I assume that you are using the queue class. ... The parent thread will spawn at least two child threads. ... Then alert Child Thread 2 that there is a message in the queue. ...
    (microsoft.public.dotnet.languages.vb)