[英]pthread_join() fail on ios
I am working on a multithread project on IOS. 我正在IOS上进行多线程项目。 In my project a pthread joining is failing sometime.
在我的项目中,pthread连接有时会失败。
pthread_join(thread_id, NULL) == 0
Note: this is only occurring on IOS and it is random. 注意:这仅在IOS上发生,并且是随机的。 What can be the reason to failing the join operation.
联接操作失败的原因可能是什么。
Man page Says: 手册页说:
ERRORS pthread_join() will fail if: 错误如果出现以下情况,pthread_join()将失败:
[EDEADLK] A deadlock was detected or the value of thread speci-
fies the calling thread.
[EINVAL] The implementation has detected that the value speci-
fied by thread does not refer to a joinable thread.
[ESRCH] No thread could be found corresponding to that speci-
fied by the given thread ID, thread.
I had the same problem, and have found a simple solution: don't call pthread_detach(). 我遇到了同样的问题,并且找到了一个简单的解决方案:不要调用pthread_detach()。 According to documentation, pthread_detach moves the tread into a state where it can no longer be joined, so pthread_join fails with EINVAL.
根据文档,pthread_detach将胎面移到了无法再连接的状态,因此pthread_join失败并显示EINVAL。
The source code could look something like this: 源代码可能如下所示:
pthread_t thread;
pthread_attr_t threadAttr;
bool run = true;
void *runFunc(void *p) {
while (run) { ... }
}
- (void)testThread {
int status = pthread_attr_init(&threadAttr);
NSLog(@"pthread_attr_init status: %d", status);
status = pthread_attr_setdetachstate(&threadAttr, PTHREAD_CREATE_JOINABLE);
NSLog(@"pthread_attr_setdetachstate status: %d", status);
status = pthread_create(&thread, &threadAttr, &runFunc, (__bridge void *)self);
NSLog(@"pthread_create status: %d", status);
/* let the thread run ... */
run = false;
status = pthread_join(thread, NULL);
NSLog(@"pthread_join status: %d == %d, ?", status, EINVAL);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.