Benches macOS

4

How to obtain the thread id?

apple Verified

Imported from the Apple Support Forums via the Wayback Machine 🔗

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

4 replies

Sign in or join Beachball Help to reply.

  1. 0

    etresoft

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    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.

  2. 0

    moclaid

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    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.

  3. 0

    etresoft

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    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.

  4. 0

    moclaid

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Yes, that is a good advice.I didn´t know ThreadViewer.

    As a recent MacOSX programmer coming from UNIX world I´m used to try command line utilities before looking GUI apps 😟 . ThreadView is great, Shark is more difficult to use and I´m not sure if I interpret its results correctly.

    Thanks