Chinaunix

标题: pthread_t 类型变量, 形参传值错误问题! [打印本页]

作者: cxytz01    时间: 2013-01-12 16:50
标题: pthread_t 类型变量, 形参传值错误问题!
本帖最后由 cxytz01 于 2013-01-12 17:01 编辑

有一程序:
1.需要将pthread_create创建的线程id,保存至pthread_t类型的数组中。
2.该创建的线程运行后,使用pthread_self获取自己的tid,然后传入一函数,该函数从上面的数组找到匹配的tid,然后返回数组的index。

问题来了,查找匹配tid的函数提示找不到匹配的tid!!!


我的系统(64bits)上pthread_t 定义如下:
typedef unsigned long int pthread_t

编译时如果使用%x打印pthread_t会出现警告信息:
thread-pool.c:77: warning: format ‘%x’ expects type ‘unsigned int’, but argument 3 has type ‘pthread_t’

如果使用%lu打印pthread_t不会出现警告。


gdb调试信息如下:       使用gdb调试发现,pthread_self获取的tid同pthread_create生成tid一致,也和保存至数组的tid一致,但是传参的时候错了!不解。

//创建线程,tid保存至thread_info [ i ].thread_id中
71                        err = pthread_create(&this->thread_info [ i ].thread_id, NULL, tp_work_thread, this);
(gdb) n

//线程启动,gdb自动打印出来的信息
[New Thread 0x7ffff783c700 (LWP 26655)]

//我程序中使用printf打印出来的信息
77                        printf("tp_init: creat work thread[%d] 0x%x, %lu\n", i, this->thread_info  [ i ].thread_id, this->thread_info [ i ].thread_id);
(gdb)
tp_init: creat work thread[0] 0xf783c700, 140737345996544

//我使用gdb命令打印出来的信息
(gdb) print /x this->thread_info [ i ].thread_id
$1 = 0x7ffff783c700
(gdb) print this->thread_info [ i ].thread_id
$2 = 140737345996544

//进入线程函数,线程生成自己的tid
300                curid = pthread_self();                  curid类型为pthread_t
(gdb) n

//我的程序使用printf打印出来的信息
302                printf("cuid = 0x%x, %lu\n", curid, curid);
(gdb) n
cuid = 0xf783c700, 140737345996544

//我使用gdb命令打印出来的信息
(gdb) print /x curid
$3 = 0x7ffff783c700
(gdb) print curid
$4 = 140737345996544

进入该函数,根据匹配到的tid,查找数组的index
304                nseq = this->get_thread_by_id(this, curid);


//函数声明 int tp_get_thread_by_id(xxx *this, pthread_t tid);

tp_get_thread_by_id (this=0x604010, tid=4152608512            --------  看, 这里就不对了!!!!!!) at thread-pool.c:184
184                printf("tid = 0x%x, %lu\n", tid, tid);
(gdb) n
tid = 0xf783c700, 4152608512
186                for (i = 0; i < this->cur_th_num; i++) {
(gdb) print tid
$5 = 4152608512


(gdb) print /x tid
$6 = 0xf783c700
作者: cxytz01    时间: 2013-01-12 17:14
本帖最后由 cxytz01 于 2013-01-12 17:41 编辑

唉,搞定了。

这个函数在源文件中声明成这种形式: int tp_get_thread_by_id(xxx *this, pthread_t tid), 但是在.h中却把tid声明成int了。  编译时,gcc也没报错。

害我调了一天。




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2