有节制地仅使用一个线程的CPU使用率 [英] Throttle CPU usage of only one thread intentionally

查看:116
本文介绍了有节制地仅使用一个线程的CPU使用率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于SO的其他几个类似的问题,但都没有令人满意/相关的答案.

There are a couple of other similar questions on SO, but none of them have satisfactory/relevant answers.

我有一个单线程的C ++程序,该程序本质上是这样的:

I have a single threaded C++ program which does something essentially like this:

while(true) {
    //do things which are cpu bound
}

问题是,当我运行此程序时,它通常是我整个计算机上唯一正在进行活动的进程.

The problem is that when I run this program it is usually the only process on my whole computer that is doing active work.

没有其他要安排的时间,操作系统自然会大量安排该程序,而我的CPU使用率却突飞猛进,我不希望.当然,我的粉丝也开始呼呼,这特别令人讨厌.

With nothing else to schedule, the OS naturally schedules this program heavily and my CPU usage shoots through the roof, which I do not want. Naturally, my fan starts whirring too which is especially annoying.

我要解决的方法是在循环内添加sleep(100)(对我而言这是正确的),但是尽管它起作用了,但它使我感到烦恼,这不是技术上正确的解决方案.

The way I am combating this is to add sleep(100) inside the loop (this is fine in terms of correctness for me), but while it works, it annoys me that it's not the technically correct solution.

这是因为如果我想睡很长时间,并且计算机上还有很多其他进程在运行,那么这将不是一个很好的解决方案,因为sleep并不准确,而且也不是允许其他进程在我的程序仍被安排时使用CPU-但这不是我的真正问题.

This is because if I wanted to sleep for a very accurate amount of time and I had lots of other processes running on my computer this would not be a good solution, as sleep is not accurate and it also doesn't allow other processes to use the CPU while my program is still scheduled - but this is not my real problem.

假设这是唯一正在运行的程序,什么是不使用sleep减少CPU使用率的正确解决方案?

What would be the correct solution to reduce CPU usage without using sleep, assuming this is the only program that is actively running?

推荐答案

这里有3种情况需要考虑.

There are 3 cases to consider here.

轮询循环

对于轮询循环,正确"解决方案更像:

For polling loops, the "correct" solution is more like:

while(true) {
    wait_for_something();
    //do things which are cpu bound
}

问题在于确定您应该等待的时间(时间,按键,垂直同步信号,接收到的数据包....?);然后尝试找到可以真正满足您需求的东西.

The problem is deciding what you should be waiting for (time, a key press, a vertical sync signal, a received packet, ....?); and then trying to find something that actually does what you need.

具有终端条件的CPU绑定

在这种情况下,正确"的解决方案更像是:

For this case, the "correct" solution would be more like:

do {
    //do things which are cpu bound
} while (running);

在这种情况下,您可以(应该?)将线程的优先级设置为非常低的优先级",以免干扰其他线程.当只有低优先级的线程正在运行时,操作系统可以决定是否(或不降低)CPU功率/速度.但这不是您的决定(这是操作系统的决定,希望有一些用户/管理员电源管理控件).

In this case you may (should?) set the thread's priority to "very low priority" so that it doesn't interfere with other threads. The OS can decide whether to (or not to) reduce CPU power/speed when there's only low priority threads running; but that's not your decision (it's OS's decision, hopefully with some user/admin power management controls).

无终端条件的CPU绑定

这种情况极少发生(非常罕见,以至于我无法想到一个合理的情况,而且我什至不确定它在实践中是否存在).通常,如果您认为您的情况是"CPU绑定而没有终端条件",则可能是错误的,并且可能是其他情况之一.

This case is extremely rare (so rare that I can't think of a plausible scenario and I'm not even sure it exists in practice). Mostly if you think your case is "CPU bound without terminal condition" you're probably wrong and it's probably one of the other cases.

如果实际上是没有终端条件的CPU绑定",那么除了没有条件(您仍然希望使用低优先级的线程,并且只有电源管理)时,它几乎与有终端条件的CPU绑定"相同.低优先级线程的运行仍然不是您的决定.

If it actually is "CPU bound without terminal condition", then it's mostly just the same as "CPU bound with terminal condition" except there's no condition (you'd still want a low priority thread, and power management when there's only low priority threads running is still not your decision).

这篇关于有节制地仅使用一个线程的CPU使用率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆