Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Name a thread? but how? through tss?

0 views
Skip to first unread message

Tobias Eriksson

unread,
Feb 5, 2001, 5:01:25 AM2/5/01
to
Hi
I'm trying to put a name on my threads, so that it should be easy to pin
point threads during debugging.
I've been searching the web, and posting messages on news
comp.programming.threads it should be possible but how.

As I understand it is possible, I've got an answer from someone who said it
would be possible through the "tss" thread specific storage. The proof that
it actually is possible is the "info threads" command in gdb, where I will
get a list of threads where the two first threads always are named "initial
thread" and "manager thread".

Anyone who knows anything about this?

Please let me know

Regards
Tobias


Tobias Eriksson

unread,
Feb 5, 2001, 10:19:21 AM2/5/01
to
Hi Stefan
Thanx for the answer, but as I understand your exampl. the tss is on an
application level what I am after is on a more system/application level.
I.e. When I debug my program I want to see the names of all the threads in
the list of threads as below, now only (initial thread and manager thread
are named):

(original printout from gdb)

(gdb) info threads
6 Thread 3763 0x401a29da in sigsuspend () from /lib/libc.so.6
5 Thread 3762 0x4021fe42 in accept () from /lib/libc.so.6
4 Thread 3761 0x40201181 in nanosleep () from /lib/libc.so.6
3 Thread 3760 0x401a29da in sigsuspend () from /lib/libc.so.6
* 2 Thread 3758 (initial thread) 0x401d3b3c in free () from
/lib/libc.so.6
1 Thread 3759 (manager thread) 0x40219530 in poll () from
/lib/libc.so.6
(gdb)


What I want is it to say:
(modified printout from gdb)

6 Thread 3763 (customer access thread) in sigsuspend () from
/lib/libc.so.6
5 Thread 3762 (connection thread) in accept () from /lib/libc.so.6
4 Thread 3761 (ice-cream maker thread) in nanosleep () from /lib/libc.so.6
3 Thread 3760 (producer thread) 0x401a29da in sigsuspend () from
/lib/libc.so.6
* 2 Thread 3758 (initial thread) 0x401d3b3c in free () from
/lib/libc.so.6
1 Thread 3759 (manager thread) 0x40219530 in poll () from
/lib/libc.so.6

Since they have been able to do it for the first two thread it should to my
mind also be possible to set the others.

Rehards
Tobias


-----Original Message-----
From: Stefan Seefeld [mailto:see...@sympatico.ca]
Sent: den 5 februari 2001 17:00
To: ERIKSSON,TOBIAS (A-Sweden,ex1)
Subject: Re: How do I set a name(string) on a thread


"ERIKSSON,TOBIAS (A-Sweden,ex1)" wrote:
>
> Hi
> I've been searching the web for tss etc. but haven't really come to any
> conclusion, perhaps you could point me to some place or some command.

the relevant posix functions are

pthread_key_create, pthread_key_delete, pthread_setspecific,
pthread_getspecific

Let's look at an example. Assuming you want to be able to write debugging
messages
that contain the id of the thread they are generated in:

static long key;

static void destructor(void *data) { free(data);}
static void create() { pthread_key_create(&key, destructor);}
static void destroy() { pthread_key_delete(key);}

static counter;

long thread_id()
{
unsigned long *_id = (long *)pthread_getspecific(key);
if (!_id)
{
// insert a lock here
if (!_id)
{
_id = malloc(sizeof(long));
*_id = counter++;
pthread_setspecific(key, _id);
}
// remove lock here
}
return *_id;
};

The idea is simple: use a global variable ('key'), that is visible
to all threads, to index into a thread specific memory segment, such that
the
value being stored there is different for each thread. After the key is
generated,
you can look up the associated memory inside each thread. The returned
pointer will
initially be zero, so you have to put your data into it (for each thread !).

Hope that helps, Stefan

Tobias Eriksson wrote in message
<98136729...@cswreg.cos.agilent.com>...

0 new messages