【发布时间】:2020-05-02 07:30:48
【问题描述】:
假设我创建了一个 pthread 为 pthread_t lift_3; 和 pthread_create(&lift_1, NULL, lift, share);。当它进入lift() 时,如何获得打印线程实际名称的功能?还是给线程起个名字?
我曾尝试使用pthread_self() 来获取 id,但它却给出了随机数
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void* lift(void* ptr)
{
printf("thread name = %c\n", pthread_self());
pthread_exit(NULL);
return NULL;
}
int main()
{
pthread_t lift_1; // declare thread
pthread_create(&lift_1, NULL, lift, NULL);
pthread_join(lift_1, NULL);
return 0;
}
预期的结果应该是thread name = lift_1
【问题讨论】:
-
lift_1是变量的名称,是运行时不存在的内部符号。线程由 ids(大致数字)标识。看看stackoverflow.com/questions/21091000/… 和stackoverflow.com/questions/9221939/…