From: Logan Capaldo Date: 2005-08-29T22:41:47+09:00 Subject: Re: Associating a name with a thread? 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