- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在处理一个特别令人讨厌的错误,它会导致主线程达到 %100 并且 UI 完全卡住。
本质上,这是一个“模拟”,其中 100 个节点实例被创建为“单元”,另外 2 个节点被创建为攻击者和防御者。这些细胞有两种属性,成瘾和烦恼,攻击者和防御者每秒都会修改它们。
我运行一个计时器,对每个攻击者和防御者调用 fire() 方法,并传递随机选择的单元格作为参数。
攻击者中的fire()方法将根据时间T(即自模拟启动以来耗时)计算自己的功率,并在传递的单元上调用increaseAddiction()和increaseAnnoyance()方法,将其计算出来力量。防御者单元的 fire() 方法也会发生同样的事情,但这次调用 decreaeAddiction() 和 reduceAnnoyance() 方法。
力量计算不是线性函数,并且对于攻击者和防御者来说,创造统治时刻的攻击者和防御者是不同的。细胞节点根据其成瘾和烦恼程度进行着色,使其成为有趣的动画。
在模拟开始运行一段时间 T 后,问题出现。我能够挑出“self.annoyanceLevel = newLevel”行作为罪魁祸首,删除后代码不会显示此行为。此外,当从攻击者或防御者身上删除 fire() 方法时,问题不会发生,这导致我怀疑存在竞争条件,因此我尝试依次调用这些方法,而不是同时调用它们,但这没有帮助。我尝试创建一个新的调度队列并从那里调用它们,但没有帮助。我尝试通过将单元格标记为“inUse”来创建锁定/解锁机制,直到 setAnnoyance() 返回,但这没有帮助,我尝试通过将更改添加到数组并单独调用 setAnnoyanceLevels() 来使用来查询更改que 数组中的值,但没有帮助。我找不到任何除以零之类的东西,或者其他逻辑问题,例如意外的无限循环。
我尝试对导致问题的值进行硬编码(显然当它从 63 更改为 100 时会挂起),这解决了问题,但是,这对我没有帮助,因为我不需要硬编码的数字。
这是函数的简化版本
Timer.scheduledTimer(timeInterval: 0.2, target: self, selector: #selector(fireAtCells), userInfo: nil, repeats: true)
/* The methods that run the simulation */
@objc func fireAtCells(){
for bot in GameEngine().data.attackBots{
var targetCells = GameEngine().data.cells.filter { cell in
return cell.addictionLevel < 100
}
let pickedCell = Int.random(in: 0 ... targetCells.count - 1)
bot.fire(cell: targetCells[pickedCell])
}
}
for bot in GameEngine().data.defenceBots{
var targetCells = GameEngine().data.cells.filter { cell in
return cell.addictionLevel > 0 || cell.annoyanceLevel > 0
}
let pickedCell = Int.random(in: 0 ... targetCells.count - 1)
bot.fire(cell: targetCells[pickedCell])
}
}
}
/* These are the methods on the attacker and defender that calculate the power etc. and increaseAddicton or decreaseAnnoyance, decreaseAddiction methods on the Cells */
func fire(cell: Cell){
let gameStatus = GameEngine()
let pickBullet = Int.random(in: 0 ... bullets.count - 1)
let bullet = bullets[pickBullet]
switch bullet {
case .addict:
cell.decreaseAnnoyance(50)
cell.increaseAddiction(Int(gameStatus.status.attackPower))
bulletColor = .red
case .annoy:
let power = Int(gameStatus.status.attackPower)
cell.increaseAnnoyance(power)
}
}
/* The methods in the Cell objects that adjust it's own properties when called from the attacker or defender, addiction methods are pretty much the same, so ommiting them */
func increaseAnnoyance(_ power: Int){
self.setAnnoyanceLevel(power + self.annoyanceLevel)
}
func decreaseAnnoyance(_ power: Int){
self.setAnnoyanceLevel(self.annoyanceLevel - power)
}
func setAnnoyanceLevel(_ level : Int){
let newLevel = (level > 100) ? 100 : (level < 1 ? 1 : level)
self.annoyanceLevel = newLevel
self.wiggle(interval: 0.5 - 0.004 * Double(level), strength: 20)
self.colorize(color: UIColor.yellow, level: level)
}
更新:我能够在 CPU 达到 %100 之前拦截执行,看起来计时器行为不正常。在increaseAnnoyance()返回并且计时器准备触发FireAtCells()方法之后,它不知何故陷入了无限循环。在 %100 问题之前的最后一个循环中,我可以看到 Timer 对象从未真正完成初始化,也没有调用 ForeAtCells 方法,而是卡住了。
更新 2 好吧,奇怪的是,计时器上次从未完成执行,但 %100 CPU 似乎是由 Spritekit 节点上的某些 SKAction 使用的计时器引起的。 SpriteKit Actions 从不访问这些属性,我不明白为什么只有在以编程方式更改对象属性时才会导致问题。
最佳答案
尝试一下,如果消除了双循环,并删除了计时器:
let action = SKAction.repeatForever(SKAction.sequence([SKAction.run{fireAtCells()},SKAction.wait(forDuration:0.2)]))
scene.run(action)
/* The methods that run the simulation */
@objc func fireAtCells(){
var attackTargetCells = GameEngine().data.cells.filter { cell in
return cell.addictionLevel < 100
}
for bot in GameEngine().data.attackBots{
let pickedCell = attackTargetCells[Int.random(in: 0 ... attackTargetCells.count - 1)]
bot.fire(cell: pickedCell)
if pickedCell.addictionLevel >= 100 {
attackTargetCells.remove(pickedCell)
}
}
let defenceTargetCells = GameEngine().data.cells.filter { cell in
return cell.addictionLevel > 0 || cell.annoyanceLevel > 0
}
for bot in GameEngine().data.defenceBots{
let pickedCell = defenseTargetCells[Int.random(in: 0 ... defenseTargetCells.count - 1)]
bot.fire(cell: pickedCell)
if cell.addictionLevel <= 0 || cell.annoyanceLevel <= 0 {
defenseTargetCells.remove(pickedCell)
}
}
}
/* These are the methods on the attacker and defender that calculate the power etc. and increaseAddicton or decreaseAnnoyance, decreaseAddiction methods on the Cells */
func fire(cell: Cell){
let gameStatus = GameEngine()
let pickBullet = Int.random(in: 0 ... bullets.count - 1)
let bullet = bullets[pickBullet]
switch bullet {
case .addict:
cell.decreaseAnnoyance(50)
cell.increaseAddiction(Int(gameStatus.status.attackPower))
bulletColor = .red
case .annoy:
let power = Int(gameStatus.status.attackPower)
cell.increaseAnnoyance(power)
}
}
/* The methods in the Cell objects that adjust it's own properties when called from the attacker or defender, addiction methods are pretty much the same, so ommiting them */
func increaseAnnoyance(_ power: Int){
self.setAnnoyanceLevel(power + self.annoyanceLevel)
}
func decreaseAnnoyance(_ power: Int){
self.setAnnoyanceLevel(self.annoyanceLevel - power)
}
func setAnnoyanceLevel(_ level : Int){
let newLevel = (level > 100) ? 100 : (level < 1 ? 1 : level)
self.annoyanceLevel = newLevel
self.wiggle(interval: 0.5 - 0.004 * Double(level), strength: 20)
self.colorize(color: UIColor.yellow, level: level)
}
关于ios - 当一个对象的属性同时被另外两个对象设置时,CPU 会达到 %100,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56575151/
我想知道在谈到 CPU 使用率和 CPU 利用率时,术语是否存在科学差异。我觉得这两个词都被用作同义词。它们都描述了 CPU 时间和 CPU 容量之间的关系。 Wikipedia称之为 CPU 使用率
我研究了一些关于处理器和 Tomasulo 算法的指令重新排序的内容。 为了更深入地了解这个主题,我想知道是否有任何方法可以(获取跟踪)查看为给定程序完成的实际动态重新排序? 我想给出一个输入程序并查
我有一台配备 2 个 Intel Xeon CPU E5-2620 (Sandy Bridge) 和 10Gbps 82599 NIC(2 个端口)的服务器,用于高性能计算。从 PCI 关联性中,我看
您能详细解释一下“用户 CPU 时间”和“系统 CPU 时间”吗?我读了很多,但我不太理解。 最佳答案 区别在于时间花在用户空间还是内核空间。用户 CPU 时间是处理器运行程序代码(或库中的代码)所花
我想知道如何识别 CPU 是否与 ARM v5 指令集兼容。 假设 ARM v7 指令与 ARM v5 兼容是否正确? 最佳答案 您可以阅读 CPUID base register获得PARTNO。然
我目前在具有多个六核 CPU 的服务器上使用 C 多线程。我想将我的一些线程的亲和性设置为单个 CPU 的各个核心。我使用过 pthread_setaffinity_np() 和 sched_seta
1) 独占时间是在方法中花费的时间2) 包含时间是在方法中花费的时间加上在任何被调用函数中花费的时间3)我们称调用方法为“ parent ”,称方法为“子”。引用链接:Click here 这里的问题
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 5 年前。 Improve this ques
好的,所以编译器可以出于性能原因自由地重新排序代码片段。让我们假设一些代码片段,在没有应用优化的情况下直接翻译成机器代码,看起来像这样: machine_instruction_1 machine_i
我在 zabbix 中有以下默认图表,但我不知道如何解释这些值。谁能解释一下? 最佳答案 操作系统是一件非常忙碌的事情,尤其是当你让它做某事时(即使你没有做)。当我们看到一个活跃的企业环境时,总会发生
换句话说,L1、L2、L3 等缓存是否总是反射(reflect) CPU的字节序 ? 或者总是将数据存储在某些 的缓存中更有意义吗?特定字节序 ? 有没有总体设计决策 ? 最佳答案 大多数现代缓存不会
我想知道当前的 cpus 是否避免在其中至少一个为零时将两个数字相乘。谢谢 最佳答案 这取决于 CPU 和(在某些情况下)操作数的类型。 较旧/较简单的 CPU 通常使用如下乘法算法: integer
我有一个 CUDA 应用程序,它在一台计算机(配备 GTX 275)上运行良好,而在另一台配备 GeForce 8400 的计算机上运行速度慢了大约 100 倍。我怀疑有某种回退使代码实际上在 CPU
例如,对于 8 位 CPU,堆栈大小预计为 8 位宽,16 位 CPU 与 16 位堆栈宽度,以及 32 位、64 位 CPU,等等。是否适用于所有架构? 最佳答案 CPU 具有数据总线和地址总线。它
实现 SIMD 是否需要多核 CPU? 在阅读有关 SIMD 的维基百科时,我发现了以下短语“多处理元素”。那么这句话和“多核CPU”有什么区别呢? 最佳答案 不,每个内核通常都可以执行指令集中的大多
我遗漏了一些基本的东西。 CPU 流水线:在基本层面上,为什么指令需要不同数量的时钟周期才能完成,为什么有些指令在多级 CPU 中只需要 1 个周期? 除了明显的“不同的指令需要不同的工作量才能完成”
超线程 CPU 是实现并行还是仅实现并发(上下文切换)? 我的猜测是没有并行性,只有通过上下文切换的并发性。 最佳答案 单个物理 CPU 具有超线程的核心显示为 两个逻辑 CPU 到操作系统。 CPU
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a softwar
背景是这样的:下周我们的办公室将有一天因为维护而没有暖气。预计室外温度在 7 至 12 摄氏度之间,因此可能会变冷。可移植电取暖器数量太少,无法满足所有人的需求。 但是,在我大约 6-8 平方米的办公
我开发了一个应用程序,该应用程序在我的开发箱上的三个容器中运行,该开发箱具有带超线程的四核,这意味着系统和 docker 使用 8 个核心。 容器的 CPU 分配由 docker-compose 完成
我是一名优秀的程序员,十分优秀!