使用pthread_create创建的pthread_t作为线程标识符,存在同一个标识符,在不同的时间段可能标识了两个不同的线程
demo如下:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
static void*
thread_start(void *arg)
{
const char *str = arg;
printf("%s\n", str);
return NULL;
}
int main()
{
pthread_t pid;
int ret;
const char *str;
str = "thread_1";
ret = pthread_create(&pid, NULL, &thread_start, str);
if (ret)
{
printf("pthread_create 1 error. errno:%d\n", ret);
return -1;
}
ret = pthread_join(pid, NULL);
if (ret)
{
printf("pthread_join 1 error. errno:%d\n", ret);
return -1;
}
printf("pthread_t 1:%d\n", pid);
printf("\n");
str = "thread_2";
ret = pthread_create(&pid, NULL, &thread_start, str);
if (ret)
{
printf("pthread_create 2 error. errno:%d\n", ret);
return -1;
}
ret = pthread_join(pid, NULL);
if (ret)
{
printf("pthread_join 2 error. errno:%d\n", ret);
return -1;
}
printf("pthread_t 2:%d\n", pid);
return 0;
}
运行结果:
thread_1
pthread_t 1:1245562624
thread_2
pthread_t 2:1245562624
对于如何争取获取线程id标识,请参考: