From: Michael Malone Date: 2009-03-09T12:15:11+09:00 Subject: 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. =======================================================================