免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4435 | 回复: 1

[函数] pthread_t 类型变量, 形参传值错误问题! [复制链接]

论坛徽章:
7
天蝎座
日期:2013-09-28 10:45:42双子座
日期:2013-10-16 16:27:09射手座
日期:2013-10-23 10:21:32处女座
日期:2014-09-17 16:44:332015年亚洲杯之巴林
日期:2015-04-09 17:28:01冥斗士
日期:2015-11-26 16:19:0015-16赛季CBA联赛之山东
日期:2018-03-02 23:59:31
发表于 2013-01-12 16:50 |显示全部楼层
本帖最后由 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

论坛徽章:
7
天蝎座
日期:2013-09-28 10:45:42双子座
日期:2013-10-16 16:27:09射手座
日期:2013-10-23 10:21:32处女座
日期:2014-09-17 16:44:332015年亚洲杯之巴林
日期:2015-04-09 17:28:01冥斗士
日期:2015-11-26 16:19:0015-16赛季CBA联赛之山东
日期:2018-03-02 23:59:31
发表于 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也没报错。

害我调了一天。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP