Search code examples
macospthreads

How do I get the current pthread ID?


In system.log, I can see for my process:

thread 515376 caught burning CPU! It used more than 50% CPU

I use multiple threads so I've tried printing the thread ID in the runnable method that the thread uses like:

void* runnable1(void* ptr)
{
    pthread_t tid = pthread_self();
    printf("HELLO from thread runnable1 with id : %ld\n", tid);

    ...
}

However, I get an ID like:

HELLO from thread runnable1 with id : 4488212480

which is very different than the one from system.log.

The question is how do I get the thread ID the way it appears in system.log?


Solution

  • Try:

    uint64_t tid;
    pthread_threadid_np(NULL, &tid);
    printf("HELLO from thread runnable1 with id : %ld\n", tid);