免费注册 查看新帖 |

Chinaunix

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

[C] 请问这个简单的程序错在哪里? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-05-25 11:13 |只看该作者 |倒序浏览

  1. 1 #include <stdio.h>
  2.   2 #include <stdlib.h>
  3.   3 #include <pthread.h>
  4.   4
  5.   5 void cleanup(void *arg)
  6.   6 {
  7.   7     printf("cleanup :%s\n",(char *)arg);
  8.   8 }
  9.   9
  10. 10 void *thread1(void *arg)
  11. 11 {
  12. 12     pthread_cleanup_push(cleanup,"thread1");
  13. 13     pthread_exit((void *)1);
  14. 14 }
  15. 15
  16. 16 int main()
  17. 17 {
  18. 18     pthread_t tid1;
  19. 19     pthread_create(&tid1, NULL, thread1, NULL);
  20. 20     pthread_join(tid1, NULL);
  21. 21     exit(0);
  22. 22 }
复制代码


报错:
-laptop:~/test$ gcc pt1.c -lpthread
pt1.c: In function ‘thread1’:
pt1.c:16: error: expected ‘while’ before ‘int’
pt1.c:22: error: expected declaration or statement at end of input
pt1.c:22: error: expected declaration or statement at end of input

论坛徽章:
0
2 [报告]
发表于 2009-05-25 11:37 |只看该作者
线程里面调用的函数哪里钻出来的??????
一没见你声明,而没见你定义
pthread_cleanup_push(cleanup,"thread1");

论坛徽章:
0
3 [报告]
发表于 2009-05-25 11:40 |只看该作者
原帖由 mgqw 于 2009-5-25 11:37 发表
线程里面调用的函数哪里钻出来的??????
一没见你声明,而没见你定义



第5行至第8行不是cleanup的定义么?

[ 本帖最后由 lazywrite 于 2009-5-25 13:10 编辑 ]

论坛徽章:
0
4 [报告]
发表于 2009-05-25 12:58 |只看该作者

回复 #3 lazywrite 的帖子

汗 名字都不一样~~~

论坛徽章:
0
5 [报告]
发表于 2009-05-25 13:04 |只看该作者
原帖由 lsupper 于 2009-5-25 12:58 发表
汗 名字都不一样~~~



阿哦,那是我回复的时候写错了。。。。。。。。。。

[ 本帖最后由 lazywrite 于 2009-5-25 13:05 编辑 ]

论坛徽章:
0
6 [报告]
发表于 2009-06-03 00:55 |只看该作者
这是因为pthread_cleanup_的2个函数可以实现为宏,这样它们就必须配对使用,把thread1函数改成下面这样就行了
void *thread1(void *arg)
{
     pthread_cleanup_push(cleanup,"thread1");
     pthread_exit((void *)1);
     pthread_cleanup_pop(0);
}

评分

参与人数 1可用积分 +10 收起 理由
samon_fu + 10 我很赞同。

查看全部评分

论坛徽章:
0
7 [报告]
发表于 2009-06-03 05:40 |只看该作者
其实关于pthread_cleanup_push和pthread_cleanpop接口,大家可以参考下下面这个网址。
pub早年对于线程接口函数分析的还是很不错的:
http://www.ibm.com/developerwork ... ix_threadapi/part4/

论坛徽章:
0
8 [报告]
发表于 2009-06-03 15:29 |只看该作者
原帖由 sqrabs 于 2009-6-3 00:55 发表
这是因为pthread_cleanup_的2个函数可以实现为宏,这样它们就必须配对使用,把thread1函数改成下面这样就行了
void *thread1(void *arg)
{
     pthread_cleanup_push(cleanup,"thread1");
     pthread_e ...


exit以后的代码怎么执行,看来我要补充下pthread的知识了
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP