大致草稿——————————
思维导图
学习目标
一、线程ID的理解
1.1 引出对tid的理解
我们先来创建一个线程复习一下线程的函数:
pthread_t tid;
// 创建一个线程
pthread_create(&tid, nullptr, threadrun, (void*)"thread-1");
// 打印出新线程的tid
std::cout << "new pthread tid:" << tid << std::endl;
// 进行线程的暂停
pthread_join(tid, nullptr);
// 线程执行的任务函数
void* threadrun(void* args)
{
std::string name = static_cast<const char*>(args);
while (true)
{
std::cout << name << " is runing, tid:" << pthread_self() << std::endl;
sleep(1);
}
}
// 将一个数字转换为十六进制的字符串
std::string ToHEX(pthread_t tid)
{
char id[128];
snprintf(id, sizeof id, "0x%lx", tid);
return id;
}