免费注册 查看新帖 |

Chinaunix

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

[函数] 关于pthread_cleanup_push注册的清理函数被执行的疑问 [复制链接]

论坛徽章:
2
申猴
日期:2014-04-17 14:37:17CU十四周年纪念徽章
日期:2018-06-23 16:03:03
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-02-05 15:19 |只看该作者 |倒序浏览
网上查资料说:
pthread_cleanup_push来注册清理函数。在以下三种情形之一发生时,注册的清理函数被执行:
    1)调用pthread_exit。
    2)作为对取消线程请求(pthread_cancel)的响应。
    3)以非0参数调用pthread_cleanup_pop。
在线程宿主函数中主动调用return,如果return语句包含在pthread_cleanup_push()/pthread_cleanup_pop()对中,则不会引起清理函数的执行,反而会导致segment fault。
<--------------------------------------------------------->
但是我自己试了如果是return操作也是会调用注册清理函数,没有报什么错。有人试过吗?

论坛徽章:
4
水瓶座
日期:2013-09-06 12:27:30摩羯座
日期:2013-09-28 14:07:46处女座
日期:2013-10-24 14:25:01酉鸡
日期:2014-04-07 11:54:15
2 [报告]
发表于 2013-02-05 15:36 |只看该作者
       A cancellation clean-up handler is popped from the stack and executed in the following circumstances:

       1. When a thread is canceled, all of the stacked clean-up handlers are popped and executed in the reverse of  the
          order in which they were pushed onto the stack.

       2. When  a  thread  terminates by calling pthread_exit(3), all clean-up handlers are executed as described in the
          preceding point.  (Clean-up handlers are not called if the thread terminates by performing a return  from  the
          thread start function.)

       3. When  a  thread  calls pthread_cleanup_pop() with a nonzero execute argument, the top-most clean-up handler is
          popped and executed.     


  POSIX.1 says that the effect of using return, break, continue, or goto to prematurely  leave  a  block  bracketed
       pthread_cleanup_push() and pthread_cleanup_pop() is undefined.  Portable applications should avoid doing this.

论坛徽章:
2
申猴
日期:2014-04-17 14:37:17CU十四周年纪念徽章
日期:2018-06-23 16:03:03
3 [报告]
发表于 2013-02-05 15:46 |只看该作者
回复 2# linux_c_py_php
看了您的解答,我没能理解这个(Clean-up handlers are not called if the thread terminates by performing a return  from  the thread start function.)
看最后的文字是说尽量避免使用 return, break, continue,  goto等操作离开定义的块。

   

论坛徽章:
4
水瓶座
日期:2013-09-06 12:27:30摩羯座
日期:2013-09-28 14:07:46处女座
日期:2013-10-24 14:25:01酉鸡
日期:2014-04-07 11:54:15
4 [报告]
发表于 2013-02-05 15:47 |只看该作者
POSIX.1 says that the effect of using return, break, continue, or goto to prematurely  leave  a  block  bracketed
       pthread_cleanup_push() and pthread_cleanup_pop() is undefined.  Portable applications should avoid doing this.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP