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

本文介绍了一个使用C++实现的多线程程序示例,通过创建三个线程并赋予各自不同的ID来展示线程的基本使用方法。每个线程运行时会输出其线程ID和系统分配的线程标识符。
706

被折叠的 条评论
为什么被折叠?



