How to obtain the thread id?

Hi all:

Do you know something like C gettid() call for MacOSX ? I have used that in Linux through syscall interface, but not in MacOSX.
What does pthread_self() returns in MacOS X, a struct or a number (thread id)?


Thanks

Posted on Jun 5, 2008 10:25 AM

Reply
4 replies

Jun 5, 2008 11:05 AM in response to moclaid

It returns a pthread_t, which may or may not be an opaque type. Why do you specifically need an "ID"? There certainly is one somewhere, but it isn't part of POSIX. It would be a MacOS X-specific structure.

The man page for gettid says that the result is just the same as getpid, unless the process is in a thread group. If you aren't doing that, you can use getpid on the Mac to get the same result. This is not a "thread id", but then it isn't on Linux either.

Jun 6, 2008 12:22 AM in response to moclaid

Thanks for your comments.

I need that because I have a multithreaded app in which a couple of threads are using almost all CPU. If I can trace in my log the ids of the threads I can identify which of them are causing trouble.
Also, if i have a deadlock, for example, I can check the log to see which thread is locked with which one.
Yes, in Linux pthread_self() gives something different that gettid().This returns the pid of the LWP associated with the threads. But ps -efL <pid> gives this same LWP id, so I can identify in my log which thread is doing something.
In MacOS, ps -M -p <pid> show the threads, but all with the same pid so is useless.

Thanks again.

Jun 6, 2008 5:22 AM in response to moclaid

Why don't you use one of Xcode's performance tools? I haven't run one since Xcode 3, but I used to run Thread Viewer every now and then. It should tell you exactly what you want to know.

It is going to be quite difficult to get that thread ID. It isn't part of any standard so you'll have to poke around in some obscure MacOS X-specific header files. If I need to do that I just print out the pthread_t value, whatever it happens to be.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

How to obtain the thread id?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.