Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


Thead ID

Posted on 2004-04-22
Medium Priority
Last Modified: 2009-07-29
hi all!

i am confused regarding the thread identifier obtained using via pthread_self( ) and getpid( ). What exactly is the difference between the two and what do they imply??

Can somebody help me and explain/send a suitable link?
Question by:linux_newb
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 10895933
check this link out: http://www.talkaboutprogramming.com/group/comp.programming.threads/messages/41158.html
It contains an exmple program that you can try as well.



Expert Comment

ID: 10896197
Well pthread_self() returns the thread identifier for the calling thread and getpid() returns a process id.  The same process can fire up multiple threads each with a different thread identifier.



Author Comment

ID: 10896485
I think i didnt put my question properly..
I know pthread_self() returns thread identifier & getpid() returns process id..

What i want to know is, what  exactly is the relevance of thread id  and process id?
When i print the process id for a thread, it is different and unique to it, as is thread id..
I am using RedHat 8.
So which should be used if one is looking for a particular thread? thread id or pid??

Sorry for not framing the question correctly..
Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

LVL 45

Accepted Solution

sunnycoder earned 150 total points
ID: 10896574
In a thread you should be looking for thread id and not pid ..

In linux pthreads are implemented using clone and hence all of them are separate processes and thus all of them do have separate unique pids ... But since you are working with threads, it is better to use its interface ... this will allow your code tp be portable for platforms which do not have different pids for each thread

Author Comment

ID: 10896771

I got your point..

Still have a doubt. A created a small multithreaded prog, which loops for a while before terminating. Then executed it  twice at the same time. In two executions, PID of main process and threads are different but the thread id are identical.
Is it possible that two different processes (executing at same time) can have threads with  same thread ids?
Below is the output..

Main thread -- TID: 8192 PID: 1466
Child1 thread -- TID: 8194 PID: 1468 PPID: 1467
Child1 thread -- TID: 16387 PID: 1469 PPID: 1467
Child2 thread -- TID: 24580 PID: 1470 PPID: 1467
Child2 thread -- TID: 32773 PID: 1471 PPID: 1467
Child2 thread -- TID: 40966 PID: 1472 PPID: 1467
Child2 thread -- TID: 49159 PID: 1473 PPID: 1467

Main thread -- TID: 8192 PID: 1458
Child1 thread -- TID: 8194 PID: 1460 PPID: 1459
Child1 thread -- TID: 16387 PID: 1461 PPID: 1459
Child2 thread -- TID: 24580 PID: 1462 PPID: 1459
Child2 thread -- TID: 32773 PID: 1463 PPID: 1459
Child2 thread -- TID: 40966 PID: 1464 PPID: 1459
Child2 thread -- TID: 49159 PID: 1465 PPID: 1459

LVL 45

Expert Comment

ID: 10896827
Honestly, I never tried it and never thought that thread-ids could be repeated in different processes ... but as your results show, they are!!! May be it internally maintains a threadid pid combination to identify a thread system-wide uniquely ...
stick to pthread_self
LVL 12

Assisted Solution

stefan73 earned 60 total points
ID: 10897629
Hi linux_newb,
> Is it possible that two different processes (executing at same time)
> can have threads with  same thread ids?

If I remember correctly, a POSIX thread ID is only valid within the parent process context. The fact that Linux creates new PIDs via clone() does not change that.

But indeed the Linux behaviour is a bit strange. I wonder how signal handling works in the case of different PIDs.


Author Comment

ID: 10914646
thanks guys!!
i'll split the points..

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question