gettid()获取线程ID测试程序
2010-11-16 15:30
344 查看
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'”错误
#include "stdio.h" #include "stdlib.h" #include "unistd.h" #include "pthread.h" #include <sys/syscall.h> #define gettid() syscall(__NR_gettid) const int M = 3; pthread_t hThread[M]; int threadId[M]; void *hello(void *ptr) { int id = *(int *)(ptr); printf("Hello! %d/n", id); printf("Thread id:%d/n", gettid()); sleep(1); } void initThreade() { int i; for (i = 0; i < M; ++i) { threadId[i] = i; } } void MyThreads() { int i; for (i = 0; i < M; i++) { pthread_create(hThread + i, NULL, hello, (void *)(threadId + i)); } for (i = 0; i < M; i++) { pthread_join(hThread[i], NULL); } } int main(){ initThreade(); MyThreads(); return 0; } g++ hello.c -o hello -lpthread
[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'”错误
相关文章推荐
- mfc 查找线程获取程序的线程ID,判断线程是否启动
- 获取当前线程的id号 gettid()
- Linux下获取线程TID的方法——gettid()
- V4L2摄像头获取单幅图片测试程序(MMAP模式)
- mgo-后续测试(指定字段,获取id)
- 详解微信小程序 登录获取unionid
- Linux获取线程id的方法学习
- LAMBDA表达式与线程及线程等待和获取线程ID
- Unix网络编程:关于编译并测试TCP时间获取的客户程序
- C#获取运行程序的进程ID
- Linux下获取线程ID的方法
- 获取线程标识函数gettid和pthread_self的区别
- 编写一个程序,开启3个线程,这3个线程的ID分别为A、B、C,每个线程将自己的ID在屏幕上打印10遍 .
- linux获取线程ID
- C#多线程--获取当前线程ID
- 原创:微信小程序java实现AES解密并获取unionId
- linux 线程创建 pthread_create函数 获取线程id
- Hacking Windows CE: 如何从线程ID获取线程名称
- VC获取进程ID 线程ID 窗口句柄