ios - SpriteKit,把一个进程放在另一个核心上?

标签 ios swift multithreading sprite-kit grand-central-dispatch

想象一下

 class NattyScene: SKScene {

您可能有一个自定义的节点字段,或者每帧都会发生的其他事情。现在想象你有一些计算,一个很好的例子可能是重心......

var globalCOG: CGPoint
func updateCOG() {
   .. say, get all the .position of all Spaceship ..
   globalCOG = .. some point
}

假设像这样的问题,把它放在另一个线程上会很有意义

  • 读取位置是线程安全的/快速的
  • 另一个核心上的另一个假定线程,了解 SpriteKit 帧(这样您就可以以通常的方式计算直到放弃时间,等等,也许您可​​能更喜欢跳过帧或其他任何东西 - 线程游戏编程的通常套路)
  • 你可以线程安全/blahblah 将 COG 全局写回到 SpriteKit 的其余部分

这是怎么回事?

  • 在 Swift4/SpriteKit 中真正的现代习语是什么
  • 如何强制它在另一个完整的物理核心上运行?

有什么想法吗?


override func update(_ currentTime: TimeInterval) {

    let x = safely get info from a thread on another physical core on the device
    print("now that's cool")
}

在另一个核心......

 func loopy() {

    let x = safely look at info on NattyScene
    let globalCOG = calculation
}

请注意,KOD 已指向 DispatchQueue,这很好 - 但有什么方法可以确保它真的在另一个核心上吗?

最佳答案

好问题,不幸的是我不认为这是可能的,主要有两个原因。

原因 1

您在 iOS 中没有这种低级别访问权限。

操作系统决定哪个线程在哪个内核上运行。 它还具有根据应用范围之外的多种条件打开和关闭内核的能力。

E.g. When in Grand Central Dispatch you write

DispatchQueue.global(qos: .background).async { }

You have no guarantee the closure will be executed on a different core.

原因2

SpriteKit 的 Game Run Loop 确实执行了一堆东西

  1. 通话更新
  2. 评估行动
  3. 模拟物理
  4. 应用约束

您的想法确实意味着在call update 阶段执行以下步骤

  1. 在“后台”线程上移动
  2. 执行一些繁重的计算
  3. 将结果返回主线程

但此时您无法保证 Game Run Loop 仍处于 call update 阶段。它可能在 evaluates actions 中,甚至可能在下一帧工作。 另一方面,渲染每一帧的时间不超过 16 毫秒

可能的解决方案

您可以充分利用当前内核允许的 64 位,而不是针对另一个 CPU 内核。 看this Q/A关于 SceneKit,其中列出了 SIMD 的优势。

关于ios - SpriteKit,把一个进程放在另一个核心上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47191582/

相关文章:

iphone - 不想为 UITableViewCell 使用 dequeueReusableCellWithIdentifier

ios - UIPickerView 在 UIView 中不显示添加的 subview

ios - 如何在 Swift 中获取选定行的值?

swift - 快速链接 Realm 中的对象的问题

Thread.sleep() 的 Java 性能问题

java - notifyAll() 抛出 IllegalMonitorStateException

ios - 以编程方式在 UIAlertController 中获取不断变化的文本字段

ios - 什么是几秒钟后消失的 ios 文本通知

ios - 为什么在 UIScrollView 中看不到 UIStackView?

iphone - 主线程上的多个 GCD 调度