swift - 如何在 Swift 中实现“无尽任务”
问题描述
我是 Swift 的新手,我没有太多的经验。问题是,当我添加以下代码时,我遇到了 CPU 使用率高的问题(Xcode 调试会话中超过 100% 的 cpu 使用率)。
为了实现无限工作线程,我使用了 GCD(许多文章中最推荐的方式),包括 while-loop 结构。
private let engineQueue = DispatchQueue(label: "engine queue", qos: .userInitiated)
public func startEngine() {
engineQueue.async {
while true {
if (self.captureManager.isSessionRunning) {
guard let frame = self.captureManager.frameQueue.popFrame(),
let buf = frame.getYPlanar() else {
continue
}
// process a frame
self.engine.processNewFrame(buf: buf, width: Int32(frame.cols!), height: Int32(frame.rows!))
}
}
}
}
而且我注意到这是一种糟糕的做法(高 CPU 使用率),肯定有比这更好的方法。
所以问题是
在这种情况下我必须使用 GCD 而不是 Thread 吗?
为此工作的最佳方法是什么?
谢谢,
解决方案
推荐阅读
- html - 我如何删除html中两张卡之间的空格
- arrays - 可以将带有附加字段的对象推入类型化数组
- spring - '错误创建bean名称'错误弹簧框架
- dax - 在dax中起作用以强制间接关系?
- c# - 最后字符为空白时的滚动问题
- android - 如何实现android spinner的重选
- google-sheets - 每当我编辑范围的任何单元格时,对所有有效行进行保护触发
- c# - async/await 方法调用有什么用?
- python - 小刻度未正确显示
- react-router-dom - 应用程序 'react app1' 在状态 LOADING_SOURCE_CODE: [qiankun] 中死亡:#react-app1 的目标容器在 react app1 加载时不存在