免费注册 查看新帖 |

Chinaunix

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

apue程序清单11-4在运行之后出现系统崩溃。。。。 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-05-30 16:09 |只看该作者 |倒序浏览
  1. #include"apue.h"
  2. #include<pthread.h>
  3. void cleanup(void *arg)
  4. {
  5.         printf("cleanup:%s\n",(char*)arg);
  6. }
  7. void *thr_fn1(void *arg)
  8. {
  9.         printf("thread 1 start\n");
  10.         char *th1,*th2;
  11.         th1="thread 1 first handler";
  12.         th2="thread 1 second handler";
  13.         pthread_cleanup_push(cleanup,th1);
  14.         pthread_cleanup_push(cleanup,th2);
  15.         printf("thread 1 push complete\n");
  16.         if(arg)
  17.                 return((void*)1);
  18.         pthread_cleanup_pop(0);
  19.         pthread_cleanup_pop(0);
  20.         return((void*)1);
  21. }
  22. void *thr_fn2(void *arg)
  23. {
  24.         printf("thread 2 start\n");
  25.         char *th1,*th2;
  26.         th1="thread 2 first handler";
  27.         th2="thread 2 second handler";
  28.         pthread_cleanup_push(cleanup,th1);
  29.         pthread_cleanup_push(cleanup,th2);
  30.         printf("thread 2 push complete\n");
  31.         if(arg)
  32.                 pthread_exit((void*)2);
  33.         pthread_cleanup_pop(0);
  34.         pthread_cleanup_pop(0);
  35.         pthread_exit((void*)2);
  36. }
  37. int main(void)
  38. {
  39.         int err;
  40.         pthread_t tid1,tid2;
  41.         void *tret;
  42.         err=pthread_create(&tid1,NULL,thr_fn1,(void*)1);
  43.         if(err!=0)
  44.                 printf("can't");
  45.         err=pthread_create(&tid2,NULL,thr_fn2,(void*)1);
  46.         if(err!=0)
  47.                 printf("can't");
  48.         err=pthread_join(tid1,&tret);
  49.         if(err!=0)
  50.                 printf("can't");
  51.         printf("pthread 1 exit code %d\n",(int)tret);
  52.         err=pthread_join(tid2,&tret);
  53.         if(err!=0)
  54.                 printf("can't");
  55.         printf("pthread 2 exit code %d\n",(int)tret);
  56.         exit(0);
复制代码
该代码是apue11-4 线程清理处理程序,书上给出的运行结果是:
thread 1 start
thread 1push complete
thread 2 start
thread 2 push complete
cleanup: thread 2 second handler
cleanup: thread 2 first handler
thread 1exit code 1
thread 2exit code 2

我在win下用cygwin模拟linux环境的运行结果是:

大家看看怎么回事..

论坛徽章:
0
2 [报告]
发表于 2012-05-30 16:10 |只看该作者
那段okokokokokokokokok是我自己为了验证执行情况而后添加进去的,可以完全忽略!!

论坛徽章:
0
3 [报告]
发表于 2012-05-30 17:39 |只看该作者
cygwin的问题,在linux下没问题的,完美执行!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP