在用到pthead_self时不能正确显示tid,求助!

内核编译和嵌入式产品的设计与开发
回复
linux0818
帖子: 20
注册时间: 2007-01-24 10:10
联系:

在用到pthead_self时不能正确显示tid,求助!

#1

帖子 linux0818 » 2007-01-24 10:17

我用gettid()可以得到线程ID,可是用到pthread_self时却出现一个天文数字,它的返回值是一个unsigned
int 呀,我在头文件中查到,结果不正确,请各位大是帮忙看下,问题出在哪?我的代码如下:

#include <stdlib.h>
#include <unistd.h>
#include <linux/unistd.h>
#include <sys/types.h>

void *pthread_function(void *arg);

int main(int argc, char **argv)
{
pthread_t mythread;
if (pthread_create(&mythread, NULL, pthread_function, NULL)) { /* 创建线程 */
printf("strerror(errno)\n");
abort();
}
if (pthread_join(mythread, NULL)) { /* 等待线程结束 */
printf("error joining thread.");
abort(); /* 不正常程序终止 exit:正常程序的终止 */
}
return 0;
}

void *pthread_function(void *arg)
{
int i;
pthread_t thread_id;
thread_id = pthread_self();
_syscall0(pid_t, gettid);
printf("my pthread ID is:%ld\n", gettid());
printf("thread_id = %ld\n", thread_id);
for (i = 0; i < 10; i++) {
printf("hello,%d!\t", i);
fflush(stdout); /* 将用户空间数据强制输出,内核以回车当作一次输出,如没有回车内核会把要输出的数据等到程序结束后一起输出。 */
sleep(1);
}
printf("\n");
return NULL;
}

编译:gcc my_pthread.c -lpthread
运行:. /a.out
运行结果:

my pthread ID is:6914
thread_id = -1210205280
hello,0! hello,1! hello,2! hello,3! hello,4! hello,5! hello,6! hello,7! hello,8! hello,9!

操作系统:ubuntu 6.10 内核:2.6.17-10-generic
linux0818
帖子: 20
注册时间: 2007-01-24 10:10
联系:

人气怎么这么少,晕死

#2

帖子 linux0818 » 2007-01-24 12:44

晕一大片
回复