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

来源:互联网 发布:程序员培训班兄弟 编辑:程序博客网 时间:2024/05/29 09:52

 

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'”错误

原创粉丝点击