获取线程号
#include <stdio.h>
#include <stdint.h>
#include <pthread.h>
int main() {
uint64_t tid64;
pthread_threadid_np(NULL, &tid64);
printf("%llu\n", tid64);
getchar();
return 0;
}
获取进程号
#include <stdio.h>
#include <pthread.h>
#include <sys/syscall.h>
#include <unistd.h>
int main() {
char **p = (char **)pthread_self();
if (p) {
printf("%d\n", *(int *)(p+18));
}
printf("%d\n", (int)getpid());
return 0;
}
获取线程与进程ID

本文介绍了如何在C语言中使用特定的系统调用来获取当前线程的线程ID和当前进程的进程ID。通过示例代码展示了两种方法:一是使用pthread_threadid_np函数获取线程ID;二是通过pthread_self结合特定偏移量获取进程ID,以及直接使用getpid函数获取进程ID。
1213

被折叠的 条评论
为什么被折叠?



