为什么意外的无限循环会增加 CPU 使用率?
我知道意外的无限循环通常会导致 CPU 使用率较高。但是,我不太明白为什么。谁能向我解释一下吗?
I know an infinite loop to the unintended kind usually causes a high CPU usage. But, I don't quite understand why. Can anyone explain that to me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

发布评论
评论(4)
~没有更多了~
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
CPU 在执行该循环(永远不会结束)时无法执行任何其他操作。即使您使用的是抢占式多任务系统(这样无限循环只会永远阻塞它自己的进程或线程),每次操作系统的抢占式调度程序将循环交给它时,循环都会“吃掉”它的时间片。下一个切片的 CPU——什么都不做,但每次都会消耗一个切片的 CPU 时间,因此大量的 CPU 被所有其他线程占用,否则这些线程可以做有用的工作。
The CPU cannot do anything else while it's executing that loop (which never ends). Even if you're using a pre-emptive multi-tasking system (so that infinite loop will only clog forever its own process or thread), the loop will "eat" its time slice each time the OS's pre-emptive scheduler hands it the CPU for the next slice -- doing nothing, but eating up one slice's worth of CPU time each and every time, so that much CPU is lost to all other threads which could otherwise be doing useful work.