C++ pthread_join()在ios上失败
我正在IOS上做一个多线程项目。 在我的项目中,pthread连接有时会失败C++ pthread_join()在ios上失败,c++,ios,pthreads,C++,Ios,Pthreads,我正在IOS上做一个多线程项目。 在我的项目中,pthread连接有时会失败 pthread_join(thread_id, NULL) == 0 注意:这只发生在IOS上,是随机的。 联接操作失败的原因是什么。手册页显示: 错误 pthread_join()将在以下情况下失败: [EDEADLK] A deadlock was detected or the value of thread speci- fies the calli
pthread_join(thread_id, NULL) == 0
注意:这只发生在IOS上,是随机的。
联接操作失败的原因是什么。手册页显示:
错误
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.
手册页上说:
错误
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.
我也遇到了同样的问题,并找到了一个简单的解决方案:不要调用pthread_detach()。根据文档,pthread_detach会将踏板移动到无法再连接的状态,因此pthread_join会因EINVAL而失败 源代码可能如下所示:
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);
}
我也遇到了同样的问题,并找到了一个简单的解决方案:不要调用pthread_detach()。根据文档,pthread_detach会将踏板移动到无法再连接的状态,因此pthread_join会因EINVAL而失败 源代码可能如下所示:
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);
}
因为你的问题非常广泛(没有代码,没有细节),答案也必须是广泛的:原因是你在某个地方,某个时候犯了错误。(要获得更详细的答案,请在您的问题中提供更多详细信息,例如an和您收到的错误消息。ios失败时不会返回错误代码?错误代码将为您提供线索:
pthread_join()
返回错误时的-1
,在本例中,将errno
设置为一个值,指定发生的错误类型。您可能希望使用perror()
或strerror()
打印一个文本,描述设置为errno
的值指示的错误。因为您的问题非常广泛(没有代码,没有细节),答案也必须宽泛:原因是你在某个地方、某个时间犯了错误。(要获得更详细的答案,请在问题中提供更多细节,例如,一个错误和你收到的错误消息。ios失败时不返回错误代码?错误代码会给你提示:pthread\u join()
返回错误时的-1
,在本例中,将errno
设置为一个值,指定发生的错误类型。您可能希望使用peror()
或strerror()
打印一个文本,描述设置为errno
的值所指示的错误。