减慢线程的最佳方法?正在使用Sleep()? [英] Best way to slow down a thread? Is using Sleep() OK?

查看:380
本文介绍了减慢线程的最佳方法?正在使用Sleep()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个C ++库,做一些严重的CPU工作(所有的数学和计算),如果留给自己的设备,将很容易消耗100%的所有可用的CPU资源(它也是多线程的,

I've written a C++ library that does some seriously heavy CPU work (all of it math and calculations) and if left to its own devices, will easily consume 100% of all available CPU resources (it's also multithreaded to the number of available logical cores on the machine).

因此,我在主计算循环中有一个回调,使用库的软件应该调用:

As such, I have a callback inside the main calculation loop that software using the library is supposed to call:

while(true)
{
    //do math here
    callback(percent_complete);
}

在回调中,客户端调用Sleep(x) 。

In the callback, the client calls Sleep(x) to slow down the thread.

最初,客户端代码是一个固定的Sleep(100)调用,但是这导致了不可靠的性能,因为一些机器比其他机器完成数学,而睡眠在所有机器上是相同的。所以现在客户端检查系统时间,如果超过1秒已经过去了(这==几次迭代),它将睡眠半秒。

Originally, the clientside code was a fixed Sleep(100) call, but this led to bad unreliable performance because some machines finish the math faster than others, but the sleep is the same on all machines. So now the client checks the system time, and if more than 1 second has passed (which == several iterations), it will sleep for half a second.

一个可以接受的方式减慢线程?我应该使用信号量/互斥量而不是Sleep(),以最大限度地提高性能?正在睡眠x毫秒每个1秒的处理工作罚款或有什么问题,我不注意?

Is this an acceptable way of slowing down a thread? Should I be using a semaphore/mutex instead of Sleep() in order to maximize performance? Is sleeping x milliseconds for each 1 second of processing work fine or is there something wrong that I'm not noticing?

我问的原因是,陷入僵局,即使任务人显示这个过程占用了〜10%的CPU。我已经探讨了硬盘和内存争用无效,所以现在我想知道,如果我减慢线程的方式是导致这个问题。

The reason I ask is that the machine still gets heavily bogged down even though taskman shows the process taking up ~10% of the CPU. I've already explored hard disk and memory contention to no avail, so now I'm wondering if the way I'm slowing down the thread is causing this problem.

谢谢!

推荐答案

睡眠对于限制应用程序应该是好的,从你的意见是你的后。

Sleep should be fine for throttling an app, which from your comments is what you're after. Perhaps you just need to be more precise how long you sleep for.

我使用这种功能的唯一软件是 BOINC客户端。我不知道它使用什么机制,但它是开源和多平台的,所以帮助自己。

The only software in which I use a feature like this is the BOINC client. I don't know what mechanism it uses, but it's open-source and multi-platform, so help yourself.

它有一个配置选项X%)。我希望实现的方式是使用平台相关的API,如 clock() GetSystemTimes() ,并将处理器时间与已经过的挂钟时间进行比较。做一些真正的工作,检查你是否超过或低于标准,如果你过了睡眠一段时间后回来。

It has a configuration option ("limit CPU use to X%"). The way I'd expect to implement that is to use platform-dependent APIs like clock() or GetSystemTimes(), and compare processor time against elapsed wall clock time. Do a bit of real work, check whether you're over or under par, and if you're over par sleep for a while to get back under.

BOINC客户端很好地与优先级,并不会导致任何性能问题的其他应用程序,即使在100%最大CPU。我使用油门的原因是否则,客户端运行CPU平局所有的时间,并提高了风扇的速度和噪音。所以我运行在风扇保持安静的水平。更好的冷却可能我不需要它: - )

The BOINC client plays nicely with priorities, and doesn't cause any performance issues for other apps even at 100% max CPU. The reason I use the throttle it is that otherwise, the client runs the CPU flat-out all the time, and drives up the fan speed and noise. So I run it at the level where the fan stays quiet. With better cooling maybe I wouldn't need it :-)

这篇关于减慢线程的最佳方法?正在使用Sleep()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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