最近搞一个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
代码如下: