Operating System - Linux
1753320 Members
6720 Online
108792 Solutions
New Discussion юеВ

Re: What is thread id thread_t data type in ia64 platform

 
SOLVED
Go to solution

What is thread id thread_t data type in ia64 platform

Hi,
Recently I faced an issue with thread ID. I find whenever I create thread , it always gives me 64-bit long int. And always I find it can not be converted to 32-bit as it will truncate.

I read some topic in google, that thread ID is just not a long int, it can be pointer or structure .

May I know what exactly thread ID is in below platform type ?
Linux Node2 2.6.10-telco-1.46-mckinley-smp #1 SMP Fri May 30 18:29:43 UTC 2008 ia64 GNU/Linux


#include
#include
#include
#include
#include
#define NUM_THREADS 5

void *PrintHello(void *threadid)
{
int tid;
tid = (int)threadid;
printf("Hello World! It's me, thread %lu!\n", pthread_self());
while(1)
{
sleep(4);
}
pthread_exit(NULL);
}

int main(int argc, char *argv[])
{
pthread_t threads[NUM_THREADS];
int rc, t;
for(t=0;t printf("In main: creating thread %d\n", t);
rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t);
if (rc){
printf("ERROR; return code from pthread_create() is %d\n", rc);
exit(-1);
}
}
pthread_exit(NULL);
}


=================================
Output
In main: creating thread 0
In main: creating thread 1
Hello World! It's me, thread 2305843009225046224!
In main: creating thread 2
In main: creating thread 3
In main: creating thread 4
Hello World! It's me, thread 2305843009233434832!
Hello World! It's me, thread 2305843009241823440!
Hello World! It's me, thread 2305843009250212048!
Hello World! It's me, thread 2305843009258600656!

6 REPLIES 6
Steven Schweda
Honored Contributor

Re: What is thread id thread_t data type in ia64 platform

Have you looked in the system and/or compiler
header files for a typedef?

"sizeof" should be able to tell you the size
of the thing, whatever it is.

> [...] I find whenever I create thread , it
> always gives me 64-bit long int. [...]

How can you tell? Counting the digits put
out by printf() using some format or other is
not a reliable method.

Re: What is thread id thread_t data type in ia64 platform

Hi Steven

pthreadtypes.h file has declaretion for it.
/* Thread identifiers */
typedef unsigned long int pthread_t;


1. I am printing as below
printf("Hello World! It's me, thread %lu!\n", pthread_self());

Above gives the numbers which can be accomordate by 64-bit. From that I concluded 64-bit

What I want to understand is the bit/bytes of thread_id we get has special meaning. I mean its not just integer. The thred_id portray a lot information ? or its holding a memory address.
Dennis Handly
Acclaimed Contributor
Solution

Re: What is thread id thread_t data type in ia64 platform

>May I know what exactly thread ID is in below platform type?

Why do you need to know? A thread_t is a thread_t and you shouldn't look any closer.

>... %lu!\n", pthread_self())
>it's holding a memory address.

You might see more by using %lx.

>Steven: Have you looked in the system and/or compiler header files for a typedef?

That's too hard. Why not write a C++ application and use "typeid(thread_t).name()" to find out what it is. :-)

Re: What is thread id thread_t data type in ia64 platform

May I know what exactly thread ID is in below platform type?

Why do you need to know? A thread_t is a thread_t and you shouldn't look any closer.
. I got a issue, where we have to find out is pthread_id is always covers up 64-bit. By mistake in our code we had trunacted it to 32-bit. It started dumping core when we used it in pthread_kill system call. From there investigation started, is pthread_id always covers up 64-bit ? If so , does it carry any significance like first 32-bit represents some characteristics of thread , last 32-bit some others. or pthread_id is a pointer to hold a strucutre. I am more confused, why it always gets 64-bit (full) ? Has the kernel defined any range for it in sysctl file(I checked but did not find it).


>... %lu!\n", pthread_self())
>it's holding a memory address.
You might see more by using %lx.

. Output is as below with %lu, %lx!
In main: creating thread 0
In main: creating thread 1
In main: creating thread 2
In main: creating thread 3
In main: creating thread 4
Hello World! It's me, thread 2305843009225046224, 2000000000ad38d0!
Hello World! It's me, thread 2305843009233434832, 20000000012d38d0!
Hello World! It's me, thread 2305843009241823440, 2000000001ad38d0!
Hello World! It's me, thread 2305843009250212048, 20000000022d38d0!
Hello World! It's me, thread 2305843009258600656, 2000000002ad38d0!

<0a/12/1a/22/2a>

Let me know if you can make out anything from %lx, Looks to me its changing in a particular pattern

>Steven: Have you looked in the system and/or compiler header files for a typedef?

That's too hard. Why not write a C++ application and use "typeid(thread_t).name()" to find out what it is. :-)
1.
printf("sizeofthread_t=%d\n", sizeof(pthread_t));
sizeofthread_t=8

2.
#include
#include
#include
#include
#include
#include
main()
{
cout << "typeid of pthread_t="<< typeid(pthread_t).name() <<"\n";
}

====
g++ t.C -lpthread
./a.out
typeid of pthread_t=m
Dennis Handly
Acclaimed Contributor

Re: What is thread id thread_t data type in ia64 platform

>is pthread_t always covers up 64-bit? ... or pthread_id is a pointer to hold a structure.

For Linux, it looks like a pointer to the thread. For HP-UX it isn't.

>Let me know if you can make out anything from %lx, Looks to me it's changing in a particular pattern

It looks like it is incrementing by 512 Kb, the thread control block + thread stack size?

>typeid of pthread_t=m

You need to call __cxa_demangle to get the type:
http://www.codesourcery.com/public/cxx-abi/abi-mangling.html
http://www.codesourcery.com/public/cxx-abi/abi.html#demangler
Mike Stroyan
Honored Contributor

Re: What is thread id thread_t data type in ia64 platform

The old linuxthreads implementation used a pthread_t that was an index into an array. It tended to be a small number.

The more current NPTL linux threads provided by hpde 2.0 use a pthread_t that is a pointer to a struct.

You certainly need to preserve the full size of a pthread_t to reliably use it with pthread_kill. The high 32 bits of the pthread_t cannot be expected to have a predictable value. And a later implementation might use a pthread_t with an even larger type. As noted in "man pthread_equal", the pthread_t type could be a structure in some implementations.