免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1881 | 回复: 5
打印 上一主题 下一主题

昨天安装了ubuntu10.10然后用vim编辑了apue里的一个关于线程的函数,遇到问题,求教! [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-06-01 14:57 |只看该作者 |倒序浏览
  1. #include"apue.h"
  2. #include<pthread.h>
  3. struct foo{
  4.         int a,b,c,d;
  5. };
  6. struct foo *fp;
  7. void printfoo(const char *s,const struct foo *fp)
  8. {
  9.         printf(s);
  10.         printf("structure at 0x%x\n",(unsigned)fp);
  11.         printf("foo.a=%d\n",fp->a);
  12.         printf("foo.b=%d\n",fp->b);
  13.         printf("foo.c=%d\n",fp->c);
  14.         printf("foo.d=%d\n",fp->d);
  15. }
  16. void thr_fn1(void *arg)
  17. {
  18.         struct foo foo={1,2,3,4};
  19.         printfoo("thread 1:\n",&foo);
  20.         pthread_exit((void *)&foo);
  21. }

  22. void thr_fn2(void *arg)
  23. {
  24.         printf("thread 2:ID is %d\n",pthread_self());
  25.         pthread_exit((void *)0);
  26. }
  27. int main(void)
  28. {
  29.         int err;
  30.         pthread_t tid1,tid2;
  31.         err=pthread_create(&tid1,NULL,thr_fn1,NULL);
  32.         if(err!=0)
  33.                 err_quit("can't create thread1: %s\n",strerror(err));
  34.         err=pthread_join(tid1,(void *)&fp);
  35.         if(err!=0)
  36.                 err_quit("can't join with thread 1: %s\n",strerror(err));
  37.         sleep(1);
  38.         printf("parent starting second thread \n");
  39.         err=pthread_create(&tid2,NULL,thr_fn2,NULL);
  40.         if(err!=0)
  41.                 err_quit("can't create thread 2:%s\n",strerror(err));
  42.         sleep(1);
  43.         printfoo("parent :\n",fp);
  44. }
复制代码
遇到的问题:
  1. test11-3.c: In function ‘printfoo’:
  2. test11-3.c:9: warning: format not a string literal and no format arguments
  3. test11-3.c: In function ‘thr_fn2’:
  4. test11-3.c:25: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘pthread_t’
  5. test11-3.c: In function ‘main’:
  6. test11-3.c:32: warning: passing argument 3 of ‘pthread_create’ from incompatible pointer type
  7. /usr/include/pthread.h:225: note: expected ‘void * (*)(void *)’ but argument is of type ‘void (*)(void *)’
  8. test11-3.c:40: warning: passing argument 3 of ‘pthread_create’ from incompatible pointer type
  9. /usr/include/pthread.h:225: note: expected ‘void * (*)(void *)’ but argument is of type ‘void (*)(void *)’
复制代码

论坛徽章:
0
2 [报告]
发表于 2012-06-01 15:08 |只看该作者
运行是可以的,但是在编译的时候总出现这个,看着好难受!

论坛徽章:
0
3 [报告]
发表于 2012-06-01 17:06 |只看该作者
这一个改成
printf"%s", s);

printf("thread 2:ID is %d\n",pthread_self());改成
printf("thread 2:ID is %lu\n",pthread_self());  //pthread_t 是unsigned int

pthread_create的第三个参数 void *(*fn)(void *),你的明显不对
void thr_fn2(void *arg)应改为void *thr_fn2(void *arg) 才对,然后里面再返回什么。thr_fn1也一样。

这个是APUE书哪页的例子? 我读的时候好像没发现哈,

论坛徽章:
0
4 [报告]
发表于 2012-06-01 17:08 |只看该作者
warning里面的提示已经很清楚了。

论坛徽章:
0
5 [报告]
发表于 2012-06-03 12:59 |只看该作者
回复 4# fdl19881 11-3


   

论坛徽章:
0
6 [报告]
发表于 2012-06-03 13:22 |只看该作者
回复 4# fdl19881
是啊,我在写的时候竟然少看了一个*,真是悲剧!

   
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP