11.3 线程标识

本文翻译自《Advanced Programming in the UNIX》

 

就像每个进程都有一个进程ID(标识)一样,每个线程也有一个线程ID。每个进程标识在系统中是惟一的,而线程标识只在它所属的进程中有效。

 

回想一下进程ID,它的数据类型是pid_t,是一个非负整数。线程ID的数据类型是pthread_t,在实现时,可以用一个结构来代表pthread_t,所以可移植的操作系统不能把它当成整数。因此,有一个函数来比较两相线程的IDs。

 

#include<pthread.h> int pthread_equal(pthread_t tid1, pthread_t tid2); 

如果线程ID相等返回非0,否则返回0;

 

一个线程,可以调用pthread_self方法来得到它自己的线程ID。

 

#include<pthread.h> pthread_t pthread_self(void); 

 

返回调用线程的线程ID

你可能感兴趣的:(11.3 线程标识)