Associating a name with a thread?

A

Andrew S. Townley

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

Robert Klemme

2005/8/29 said:
=20
Hi. Me again.
=20
I'm doing some research/work using threads with ruby. I saw this in the
ri docs for Thread#inspect:
=20
--------------------------------------------------------- Thread#inspect
thr.inspect =3D> string
------------------------------------------------------------------------
Dump the name, id, and status of _thr_ to a string.
=20
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.
=20
#<Thread:0xb7f94e94>: doing something
=20
But, what I want is to be able to do something like
=20
task1 =3D Thread.new("task1") { ... }
=20
or at least
=20
task1 =3D Thread.new { ... }
task1.name =3D "task1"
=20
to get it to show up during execution.
=20
Maybe I'm going about this the wrong way. Any pointers in the right
direction would be appreciated.

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

t =3D Thread.new do
...
end

t[:name] =3D "my thread"

HTH

Kind regards

robert
 
L

Logan Capaldo

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
 
A

Andrew S. Townley

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

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

Robert Klemme

2005/8/29 said:
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...

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 :) and print source file and line # for each
Thread.new invocation.

Kind regards

robert
 
E

Eric Hodel

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top