免费注册 查看新帖 |

Chinaunix

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

apueSection11.5关于thread cleanup handler的一个例子不太理解 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-01-13 13:05 |只看该作者 |倒序浏览
Figure 11.5 shows how to use thread cleanup handlers. Although the example is somewhat contrived, it illustrates the mechanics involved. Note that although we never intend to pass a nonzero argument to the thread start-up routines, we still need to match calls to pthread_cleanup_pop with the calls to pthread_cleanup_push; otherwise, the program might not compile.
不太明白红色标识部分的意思
结合实例,不知道为什么在thr_fn1()和thr_fn2()中arg为非空的时候,就不需要调用pthread_cleanup_pop()

实例代码如下
  1. #include <stdlib.h>
  2. #include <stdio.h>

  3. void cleanup(void *arg)
  4. {
  5.         printf("cleanup: %s\n", (char *)arg);
  6. }

  7. void *thr_fn1(void *arg)
  8. {
  9.         printf("thrad 1 start\n");
  10.         pthread_cleanup_push(cleanup, "thread 1 first handler");
  11.         pthread_cleanup_push(cleanup, "thread 1 second handler");
  12.         printf("thread 1 push complete\n");
  13.        
  14.         if (arg)
  15.                 return ((void *)1);  
  16.         pthread_cleanup_pop(0);
  17.         pthread_cleanup_pop(0);
  18.         return ((void *)1);
  19. }

  20. void *thr_fn2(void *arg)
  21. {
  22.         printf("thread 2 start\n");
  23.         pthread_cleanup_push(cleanup, "thread 2 first handler");
  24.         pthread_cleanup_push(cleanup, "thread 2 second handler");
  25.         printf("thread 2 push complete\n");
  26.        
  27.         if (arg)
  28.                 pthread_exit((void *)2);  //为什么arg为非空的时候,就不需要调用pthread_cleanup_pop()
  29.         pthread_cleanup_pop(0);
  30.         pthread_cleanup_pop(0);
  31.         pthread_exit((void *)2);
  32. }

  33. int main(void)
  34. {
  35.         int err;
  36.         pthread_t tid1, tid2;
  37.         void *tret;

  38. //        err = pthread_create(&tid1, NULL, thr_fn1, (void *)1);
  39.         err = pthread_create(&tid1, NULL, thr_fn1, NULL);
  40.         if (err != 0)
  41.         {
  42.                 printf("can't create thread 1:%s\n", strerror(errno));
  43.                 exit(0);
  44.         }

  45.         err = pthread_create(&tid2, NULL, thr_fn2, (void *)2);
  46. //    err = pthread_create(&tid2, NULL, thr_fn2, NULL);
  47.         if (err != 0)
  48.         {
  49.                 printf("can't create thread 2:%s\n", strerror(errno));
  50.                 exit(0);
  51.         }

  52.         err = pthread_join(tid1, &tret);
  53.         if (err != 0)
  54.         {
  55.                 printf("can't join with thread 1:%s\n", strerror(errno));
  56.                 exit(0);
  57.         }
  58.         printf("thread 1 exit code %d\n", (int)tret);

  59.         err = pthread_join(tid2, &tret);
  60.         if (err != 0)
  61.         {
  62.                 printf("can't join with thread 2:%s\n", strerror(errno));
  63.                 exit(0);
  64.         }
  65.         printf("thread 2 exit code %d\n", (int)tret);
  66.         exit(0);
  67. }
复制代码

[ 本帖最后由 zhuhefang2006 于 2009-1-13 16:04 编辑 ]
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP