每个线程有一个线程ID,和进程ID在整个系统中的唯一性不同,线程ID只有在它所属的进程上下文中才有意义。
线程ID使用pthread_t数据类型来表示的,实现的时候可以用一个结构来代表pthread_t数据类型,所以可移植的操作系统实现不能把它们作为整数处理。因此必须使用一个函数来对两个线程ID进行比较。
#inlcude <pthread.h> int pthread_equal(pthread_t tid1, pthread_t tid2) 返回值:若相等,返回非0数值;否则,返回0 |
---|
用结构表示pthread_t数据类型的后果是不能用一种可一直的方式打印该数据类型的值。
线程可以通过调用pthread_self函数获得自身的线程ID。
#include <pthread.h> pthread_t pthread_self(void); 返回值:调用线程的进程ID |
---|
当线程需要识别以线程ID作为标识的数据结构时,pthread_self函数可以与pthread_equal函数一起使用。