join一个detached的线程会发生什么?

最近搞一个PVR的问题, 程序总是概率性hang住, 不好调试, 捉摸不透, 看代码时确实有看到程序中存在join一个detached线程的用法, 起初觉得奇怪, 但是打印显示hang住的地方不在这里, 也没多想, 最终邮件求助得知问题就出在这里,join与detach需要干掉一个, 问题解决。

到写博客时也没想通为什么join一个detached的线程会导致概率性hang住,自己写了一个小sample看看会发生什么,将来要是明白了再回头来补充。

MAN手册pthread_join错误返回值罗列如下:

ERRORS
       EDEADLK
              A deadlock was detected (e.g., two threads tried  to  join  with each other); or thread specifies the calling thread.

       EINVAL

              thread is not a joinable thread.

       EINVAL

              Another thread is already waiting to join with this thread.

       ESRCH  

              No thread with the ID thread could be found.

我们从结果中可以看到错误pthread_join其实立刻就返回了, 不会阻塞, 且错误号为22, 经查, 22就是EINVAL

而我这个程序不存在Another thread is already waiting to join with this thread的情况, 所以pthread_join在线程为detached的情况下不阻塞, 直接返回, 错误为要join的线程不能join

代码如下:


                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值