Fat*_*tie 5 multithreading grand-central-dispatch ios sprite-kit swift
想象一下你的
class NattyScene: SKScene {
Run Code Online (Sandbox Code Playgroud)
您可能有节点的自定义字段,或每帧发生的其他内容.现在想象你有一些计算,一个很好的例子可能是重心......
var globalCOG: CGPoint
func updateCOG() {
.. say, get all the .position of all Spaceship ..
globalCOG = .. some point
}
Run Code Online (Sandbox Code Playgroud)
把它放在另一个线程上是很有意义的,假设像
这是什么交易?
有任何想法吗?
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")
}
Run Code Online (Sandbox Code Playgroud)
在另一个核心......
func loopy() {
let x = safely look at info on NattyScene
let globalCOG = calculation
}
Run Code Online (Sandbox Code Playgroud)
请注意,KOD指出DispatchQueue,这很棒 - 但有没有办法确保它真的在另一个核心?
好问题,不幸的是我认为这是不可能的,有两个主要原因。
在 iOS 中您没有这种低级访问权限。
操作系统决定哪个线程在哪个核心上运行。它还能够根据超出应用程序范围的多种条件打开和关闭内核。
例如,当你在 Grand Central Dispatch 时写
DispatchQueue.global(qos: .background).async { }您无法保证闭包将在不同的核心上执行。
SpriteKit 的游戏运行循环确实执行了一堆东西
call update您的想法确实意味着在该阶段中执行以下步骤
但此时您无法保证游戏运行循环仍处于该call update阶段。它可能在下一帧中工作evaluates actions,甚至可能在下一帧中工作。另一方面,渲染每一帧的时间不超过16 毫秒。
您可以充分利用当前核心允许的 64 位,而不是瞄准另一个 CPU 核心。查看有关 SceneKit 的Q/A,其中列出了 SIMD 的优点。