Associating a name with a thread?

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.

#<Thread:0xb7f94e94>: 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.
***************************************************************************************************

I couldn't find exactly what you are looking for, but you can use
thread local variables. So you can do

t = Thread.new do
...
end

t[:name] = "my thread"

HTH

Kind regards

robert

···

2005/8/29, Andrew S. Townley <andrew.townley@bearingpoint.com>:

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.

#<Thread:0xb7f94e94>: 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.

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

···

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.

#<Thread:0xb7f94e94>: 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.
***************************************************************************************************

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.
>
> #<Thread:0xb7f94e94>: 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.
***************************************************************************************************

It applies to all instances - however, threads created already and
threads created elsewhere won't get a name. The easiest might be to
go down the trace route :slight_smile: and print source file and line # for each
Thread.new invocation.

Kind regards

robert

···

2005/8/29, Andrew S. Townley <andrew.townley@bearingpoint.com>:

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...

Hi Robert & Logan,

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.

You really want to use ThreadGroups to organize your threads.

A thread that spawns new threads will add them to whichever ThreadGroup it is in. You can monitor the ThreadGroup's size to figure out who is spawning the new threads.

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...

All.

···

On 29 Aug 2005, at 06:50, Andrew S. Townley wrote:

--
Eric Hodel - drbrain@segment7.net - http://segment7.net
FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04