osx - 获取pthread id

6 macos pthreads

在system.log中我可以看到我的进程:

thread 515376 caught burning CPU! It used more than 50% CPU
Run Code Online (Sandbox Code Playgroud)

我使用多个线程,所以我尝试在线程使用的runnable方法中打印线程id,如下所示:

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

    ...
}
Run Code Online (Sandbox Code Playgroud)

但是我得到这样的id:

HELLO from thread runnable1 with id : 4488212480
Run Code Online (Sandbox Code Playgroud)

与system.log中的不同.

问题是,如何以system.log中出现的方式获取线程ID?

Gor*_*ail 10

尝试:

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