如何获取当前pthread ID?

问题描述 投票:0回答:1

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

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

我使用多个线程,所以我尝试在线程使用的可运行方法中打印线程 ID,例如:

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

    ...
}

但是,我得到的 ID 如下:

HELLO from thread runnable1 with id : 4488212480

这与 system.log 中的非常不同。

问题是如何按照 system.log 中显示的方式获取线程 ID?

macos pthreads
1个回答
18
投票

尝试:

uint64_t tid;
pthread_threadid_np(NULL, &tid);
printf("HELLO from thread runnable1 with id : %ld\n", tid);
© www.soinside.com 2019 - 2024. All rights reserved.