pthread_t pthread_self(); 获取线程自身ID
int pthread_equal(pthread_t threadid1, pthread_t thread2) 判断两个线程ID是否相等,返回0 不相等,非零相等。
1 #include <pthread.h>
2 #include <stdio.h>
3 int main(){
4
5 pthread_t pid1;
6 pthread_t pid2;
7
8 pid1 = pthread_self();
9 pid2 = pthread_self();
10
11 printf("pid1:%u, pid2:%u, equal:%d", (unsigned int)pid1, (unsigned int)pid2, (pthread_equal(pid1, pid2) > 0));
12
13
14
15 return 0;
16 }
17
g++ ./pthread_self.cpp -lpthread -o pthread_self
输出:
pid1:545519376, pid2:545519376, equal:1