From: "Andrew S. Townley" Date: 2005-08-29T22:50:55+09:00 Subject: Re: Associating a name with a thread? Hi Robert & Logan, I went ahead and already have a "NamedThread" class (not particularly exciting): # Hack to provide a name to the thread so we can see where # all of these critters are comming from. class NamedThread < Thread def initialize(name, *args) super(*args) @name = name end def to_s "#{@name}:#{super}" end def inspect "#{@name}:#{super}" end attr_accessor :name end This tells me that I've created as many threads as I expect, however there are still a large number of threads that are created elsewhere that don't have names. I suppose this is the wrong approach because I really want to know who created them and where. I guess I'll run the whole thing with trace and see what I get. I had hoped I wasn't missing anything obvious. Question though: if I re-open the Thread class to add a name attribute, does that apply to all, pre-existing threads or just new ones? There are 2 others that I know about, but... Anyway, thanks for the quick responses. ast On Mon, 2005-08-29 at 14:41, Logan Capaldo wrote: > On Aug 29, 2005, at 9:19 AM, Andrew S. Townley wrote: > > > > > Hi. Me again. > > > > I'm doing some research/work using threads with ruby. I saw this > > in the > > ri docs for Thread#inspect: > > > > --------------------------------------------------------- > > Thread#inspect > > thr.inspect => string > > ---------------------------------------------------------------------- > > -- > > Dump the name, id, and status of _thr_ to a string. > > > > And was wondering how you assign a meaningful name to a thread. Right > > now, I have about 2X as many threads created as I would've thought > > (using Thread.list.length). Desk-checking the code doesn't show > > anything obvious, but I have instrumented it to the point that I have > > output with the Thread information, e.g. > > > > #: doing something > > > > But, what I want is to be able to do something like > > > > task1 = Thread.new("task1") { ... } > > > > or at least > > > > task1 = Thread.new { ... } > > task1.name = "task1" > > > > to get it to show up during execution. > > > > Maybe I'm going about this the wrong way. Any pointers in the right > > direction would be appreciated. > > > > Cheers, > > > > ast > > > > ********************************************************************** > > ***************************** > > The information in this email is confidential and may be legally > > privileged. Access to this email by anyone other than the intended > > addressee is unauthorized. If you are not the intended recipient > > of this message, any review, disclosure, copying, distribution, > > retention, or any action taken or omitted to be taken in reliance > > on it is prohibited and may be unlawful. If you are not the > > intended recipient, please reply to or forward a copy of this > > message to the sender and delete the message, any attachments, and > > any copies thereof from your system. > > ********************************************************************** > > ***************************** > > > > > > Well I can't find anything that seems to indicate that threads have > this functionality, but you have two choices: > > 1) Subclass thread and add a name method. > 2) Add a name method to thread. > > either way it would proabably look like: > > class Thread # or NamedThread < Thread > attr_accessor :name > alias __old__thread__inspect inspect # not neccessary if > subclassing thread > def inspect > inspection = __old_thread__inspect # or inspection > = super if subclassing > unless @name.nil? > inspection.sub(Regexp.new("^#<#{class.name}:)0x > [a-fA-F0-9]+")) { |match| match + @name} > else > inspection > end > end > end > > > *************************************************************************************************** The information in this email is confidential and may be legally privileged. Access to this email by anyone other than the intended addressee is unauthorized. If you are not the intended recipient of this message, any review, disclosure, copying, distribution, retention, or any action taken or omitted to be taken in reliance on it is prohibited and may be unlawful. If you are not the intended recipient, please reply to or forward a copy of this message to the sender and delete the message, any attachments, and any copies thereof from your system. ***************************************************************************************************