From: ara.t.howard@... Date: 2007-02-17T00:34:07+09:00 Subject: Re: Thread.current.parent and Thread.current.ancestors On Fri, 16 Feb 2007, Erik Veenstra wrote: > It doesn't work with Thread#start (==fork)... > > Thread#start doesn't reuse Thread.new. Neither does > rb_thread_create(), which is used in e.g. TK. > > All three methods (new, start/fork and rb_thread_create()) come > together in rb_thread_alloc(). That's the place where the > parent should be set. The problem is that you can't redefine > rb_thread_alloc() in Ruby... ;[ hrrrm. good point, this is the best i've come up with: harp:~ > cat a.rb class Thread class << self alias_method "__new__", "new" def new *a, &b child '__new__', *a, &b end alias_method "__start__", "start" def start *a, &b child '__start__', *a, &b end private def child as, *a, &b parent = Thread.current send(as, *a) do |*a| Thread.current.parent = parent b.call *a end end end def parent self['parent'] end def parent= parent self['parent'] = parent end def ancestors return self['ancestors'] if self['ancestors'] list = [t = self] while((t = t.parent)) list << t end self['ancestors'] = list end end Thread.new{ Thread.new{ Thread.new{ sleep 0.42 and p [Thread.current, Thread.current.ancestors] } } } Thread.start{ Thread.start{ Thread.start{ sleep 0.42 and p [Thread.current, Thread.current.ancestors] } } } STDIN.gets harp:~ > ruby a.rb [#, [#, #, #, #]] [#, [#, #, #, #]] > I really like this Thread.current.parent or Thread.parent. > There's at least one place where I really, really need it > myself... Something for Ruby 1.8.6?... ;] RCR? -a -- we can deny everything, except that we have the possibility of being better. simply reflect on that. - the dalai lama