| 线程ID | 含义 |
|---|---|
| pthread_t | 进程内唯一的,不同进程内可能相同。 |
| pid_t | 全局唯一,不同进程内也不相同。而且是采用递增轮回法分配,短时间内启动多个线程也会具有不同的线程id。 |
测试程序如下:
#include <stdio.h>
#include <pthread.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/syscall.h>
#define gettid() syscall(__NR_gettid)
void* threadFunc(void *){
pid_t t = gettid();
printf("gettid: %lx\n", t);
}
int main()
{
pthread_t t1, t2;
pthread_create(&t1, NULL, threadFunc, NULL);
printf("%lx\n", t1);
pthread_join(t1, NULL);
pthread_create(&t2, NULL, threadFunc, NULL);
printf("%lx\n", t2);
pthread_join(t2, NULL);
return 0;
}

参考《Linux多线程服务端编程》
版权声明:本文为zxc120389574原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。