gettid()获取线程ID测试程序

 

1、第一种方法


[root@zhuliting ft]# g++ hello.c -o hello -lpthread

[root@zhuliting ft]# ./hello
Hello! 0
Thread id:17783
Hello! 1
Thread id:17784
Hello! 2
Thread id:17785

 

 

注意:

1、第5、6行不能少,否则会出现错误:error: ‘gettid’ was not declared in this scope 

2、#include <sys/types.h>代替第5、6行

http://www.kernel.org/doc/man-pages/online/pages/man2/gettid.2.html 

3、编译加上-lpthread选项,不然会出现“undefined reference to `pthread_create'”错误

没有更多推荐了,返回首页