pthread_join()在ios上失败
pthread_join() fail on ios
我正在IOS上开发一个多线程项目。在我的项目中,pthread加入有时会失败。
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_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);
}
相关文章:
- 如果没有malloc,链表实现将失败
- 模板参数替换失败,并且未完成隐式转换
- 具有默认模板参数的多态类的模板推导失败
- 视图中的参数推导失败:take_while
- 链接到自行创建的dll失败
- 使用ios:ate写入到流会覆盖现有文件
- 带有特殊路径部分的"std::filesystem::weakly_canonical"失败
- GetShortPathName在网络驱动器上使用中文文件夹时失败
- gcc和c++17的过载解析失败
- 为什么使用 P/Invoke 调用 dll 时,某些计算机中的 LoadLibrary 失败?
- 在WSL:configure_file上对config_file的每次调用都失败:配置文件时出现问题
- 使用 GCC 卸载的 OpenMP 卸载失败,并出现"Ptx assembly aborted due to errors"
- 朱奇断言失败 - iirfilter iOS
- 不同的std::random_shuffle算法导致iOS模拟器单元测试失败
- 与iOS项目上的C 库链接失败
- iOS 和 OpenCV 错误:断言在 PhaseCorrelateRes 中失败
- pthread_join()在ios上失败
- 为什么ofstream( "log.txt" , ios::app|ios::trunc);总是失败?
- 编译iOS版OpenCV失败-Cmake失败
- iOS:从c++代码构建静态库失败