Operating System - HP-UX
1753851 Members
7416 Online
108807 Solutions
New Discussion юеВ

How does a thread get its own thread id?

 
SOLVED
Go to solution
Todd Larchuk
Advisor

How does a thread get its own thread id?

GlancePlus running in HPUX 11 has a metric called THREAD_THREAD_ID that gives the unique id for each thread as seen by the kernel scheduler. How can the thread get this id so I can figure out which thread id in glance corresponds to each thread in the process. I can get the thread number, i.e. 0 is main thread, 1, 2, ..., N-1 where N is the number of threads. That doesn't help when glanceplus give something like a pid (ex. 17215). I tried getpid() but that gives the pid of the parent process.
5 REPLIES 5
harry d brown jr
Honored Contributor

Re: How does a thread get its own thread id?

Have you looked at pthread_self?? Are you trying to gather this info outside of the application?

live free or die
harry
Live Free or Die
Kirby Collins
Advisor
Solution

Re: How does a thread get its own thread id?

try pstat_getlwp (2). It can return info (including lwpid) for each of the threads associated with a process. Here's a simple example (run on 11i):

#include
#include
#include
#include

main(argc,argv)
int argc;
char *argv[];
{
int64_t mylwpid;
pid_t mypid;
int thread,ret;
struct lwp_status tbuf;

mypid = getpid();
thread = 0;

if(ret = pstat_getlwp(&tbuf,sizeof(struct lwp_status),1,thread,mypid) < 0) {
perror("pstat_getlwp");
sleep(20);
exit(-1);
}

printf("my pid is %d, lwpid is %lld\n",mypid,tbuf.lwp_lwpid);
sleep(20);

}

> cc getlwpid.c
> a.out
my pid is 25486, lwpid is 469195

Todd Larchuk
Advisor

Re: How does a thread get its own thread id?

Thank you two gentlement for your responses.

I was able to get the thread id returned by GlancePlus by using the pstat_getlwp() function. To get the thread id for each thread, first I used pthread_self() to get the thread number, then in the arguments to pstat_getlwp I set elemcount = 1 and index = pthread_self() - 1. I had to subtract 1 because pthread_self starts at 1 but lwpid apparently starts with index base 0.

Thanks again!
Mark Haye_2
New Member

Re: How does a thread get its own thread id?

I'm looking for the same information that Todd is, and for the same reasons. While the techniques detailed here are a good start, they do not seem to work reliably.

In my application, I can call pstat_getlwp passing an index equal to (pthread_self()-1) only until a thread exits. Once a thread exits, all further calls to pstat_getlwp fail with errno=3 (No such process). I think this is because pthread_self() returns a number that is monotonically increasing for the given process, whereas the index required by pstat_getlwp is bounded by the number of active threads (light-weight processes) in the process.

So, my question becomes: How do I determine the proper index of the current thread (lwp)?
Mark Haye_2
New Member

Re: How does a thread get its own thread id?

Since my previous post, I have changed my code to scan all lwp's for my pid to find the one that doesn't match the lwpid of any existing (or recently ended) threads. That lwpid must represent the thread just created. It is working reliably, but it seems like a lot more work than it should be. Any better solutions out there?

My application runs on HP-UX 11.00 and up, 32-bit and 64-bit executables.