I'm looking to improve or request my current delay / sleep method. c++

别说谁变了你拦得住时间么 提交于 2020-03-05 03:10:17

问题


Currently I am coding a project that requires precise delay times over a number of computers. Currently this is the code I am using I found it on a forum. This is the code below.

{
    LONGLONG timerResolution;
    LONGLONG wantedTime;
    LONGLONG currentTime;

    QueryPerformanceFrequency((LARGE_INTEGER*)&timerResolution);
    timerResolution /= 1000;

    QueryPerformanceCounter((LARGE_INTEGER*)&currentTime);
    wantedTime = currentTime / timerResolution + ms;
    currentTime = 0;
    while (currentTime < wantedTime)
    {
        QueryPerformanceCounter((LARGE_INTEGER*)&currentTime);
        currentTime /= timerResolution;
    }
}

Basically the issue I am having is this uses alot of CPU around 16-20% when I start to call on the function. The usual Sleep(); uses Zero CPU but it is extremely inaccurate from what I have read from multiple forums is that's the trade-off when you trade accuracy for CPU usage but I thought I better raise the question before I set for this sleep method.


回答1:


The reason why it's using 15-20% CPU is likely because it's using 100% on one core as there is nothing in this to slow it down.

In general, this is a "hard" problem to solve as PCs (more specifically, the OSes running on those PCs) are in general not made for running real time applications. If that is absolutely desirable, you should look into real time kernels and OSes.

For this reason, the guarantee that is usually made around sleep times is that the system will sleep for atleast the specified amount of time.

If you are running Linux you could try using the nanosleep method (http://man7.org/linux/man-pages/man2/nanosleep.2.html) Though I don't have any experience with it.

Alternatively you could go with a hybrid approach where you use sleeps for long delays, but switch to polling when it's almost time:

#include <thread>
#include <chrono>
using namespace std::chrono_literals;

...

wantedtime = currentTime / timerResolution + ms;
currentTime = 0;
while(currentTime < wantedTime)
{
    QueryPerformanceCounter((LARGE_INTEGER*)&currentTime);
    currentTime /= timerResolution;
    if(currentTime-wantedTime > 100) // if waiting for more than 100 ms
    {
       //Sleep for value significantly lower than the 100 ms, to ensure that we don't "oversleep"
        std::this_thread::sleep_for(50ms); 
    }
}

Now this is a bit race condition prone, as it assumes that the OS will hand back control of the program within 50ms after the sleep_for is done. To further combat this you could turn it down (to say, sleep 1ms).




回答2:


You can set the Windows timer resolution to minimum (usually 1 ms), to make Sleep() accurate up to 1 ms. By default it would be accurate up to about 15 ms. Sleep() documentation.

Note that your execution can be delayed if other programs are consuming CPU time, but this could also happen if you were waiting with a timer.

#include <timeapi.h>

// Sleep() takes 15 ms (or whatever the default is)
Sleep(1);

TIMECAPS caps_;
timeGetDevCaps(&caps_, sizeof(caps_));
timeBeginPeriod(caps_.wPeriodMin);

// Sleep() now takes 1 ms
Sleep(1);

timeEndPeriod(caps_.wPeriodMin);


来源:https://stackoverflow.com/questions/60146019/im-looking-to-improve-or-request-my-current-delay-sleep-method-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!