更新
在Linux下使用VScode可以直接找到pthread_t的定义,如下图所示
可以直接看到 pthread_t
为 unsigned long
开发环境
- VMware虚拟机 Ubuntu16.04
- C语言
问题描述
调用 pthread_self()
函数获取线程 ID 并用 printf
打印出来,需要知道 pthread_self()
函数返回值的类型。
查询步骤
- 找到
pthread_self()
函数的定义,打开命令行,输入以下命令进入帮助手册,如下图所示
man pthread_self
可以看到 pthread_self
函数定义在头文件 pthread.h
中,其返回值类型为 pthread_t
。
- 打开
pthread.h
文件去一探究竟,在命令行输入whereis pthread.h
查询pthread.h
在哪。
按提示路径打开 pthread.h
文件
pthread.h
文件引用了头文件 bits/pthreadtypes.h
,在该目录下找到 bits
文件夹下的 pthreadtypes.h
文件。
- 打开
pthreadtypes.h
文件可以即可找到pthread_t
的定义
可以看到 pthread_t
就是 unsigned long int
,在本系统下占8字节,即为uint64_t
。因此打印线程ID需要用 %lu
格式。