通过睡眠控制循环时间

时间:2018-06-28 17:48:29

标签: c++ linux time gettimeofday

我尝试使用usleep确保每个循环的执行时间为10毫秒,但有时会超过10毫秒。

我不知道如何解决此问题,是否在 usleep gettimeofday 这个案例?

请帮助我找出我错过的事情。

  

结果:0.0127289   0.0136499   0.0151598   0.0114031   0.014801

double tvsecf(){
     struct timeval tv;
     double asec;

     gettimeofday(&tv,NULL);
     asec = tv.tv_usec;
     asec /= 1e6;
     asec += tv.tv_sec;

     return asec;
}
int main(){
    double t1 ,t2;
    t1 = tvsecf();
    for(;;){
        t2= tvsecf();
        if(t2-t1 >= 0.01){
            if(t2-t1 >= 0.011)
                cout << t2-t1 <<endl;
            t1 = tvsecf();
        }
        usleep(100);
    }
}

3 个答案:

答案 0 :(得分:3)

为了避免循环开销(通常是未知的)免于不断累积错误,您可以直到休眠,而不是 for 时间持续时间。使用C ++的<chrono><thread>库,这非常容易:

#include <chrono>
#include <iostream>
#include <thread>

int
main()
{
    using namespace std;
    using namespace std::chrono;
    auto t0 = steady_clock::now() + 10ms;
    for (;;)
    {
        this_thread::sleep_until(t0);
        t0 += 10ms;
    }
}

一个人可以用更多对steady_clock::now()的调用来修饰它,以便确定迭代之间的时间,也许更重要的是,确定平均迭代时间:

#include <chrono>
#include <iostream>
#include <thread>

int
main()
{
    using namespace std;
    using namespace std::chrono;
    using dsec = duration<double>;
    auto t0 = steady_clock::now() + 10ms;
    auto t1 = steady_clock::now();
    auto t2 = t1;
    constexpr auto N = 1000;
    dsec avg{0};
    for (auto i = 0; i < N; ++i)
    {
        this_thread::sleep_until(t0);
        t0 += 10ms;
        t2 = steady_clock::now();
        dsec delta = t2-t1;
        std::cout << delta.count() << "s\n";
        avg += delta;
        t1 = t2;
    }
    avg /= N;
    cout << "avg = " << avg.count() << "s\n";
}

以上,我通过在循环中执行更多操作来增加了循环开销。但是,循环仍将每10ms唤醒一次。有时,操作系统会延迟唤醒线程,但下一次循环会自动将自身调整为更短的睡眠时间。因此,平均迭代率会自动校正为10ms。

在我的机器上,这只是输出:

...
0.0102046s
0.0128338s
0.00700504s
0.0116826s
0.00785826s
0.0107023s
0.00912614s
0.0104725s
0.010489s
0.0112545s
0.00906409s
avg = 0.0100014s

答案 1 :(得分:0)

来自usleep man page

  

任何系统活动,花费在处理呼叫上的时间或系统计时器的粒度都会稍微延长睡眠时间。

如果需要高分辨率:在Unix(或Linux)上使用C,请查看this answer,其中介绍了如何使用clock_gettime使用高分辨率定时器。

编辑:如Tobias nanosleep所述,可能是一个更好的选择:

Compared to sleep(3) and usleep(3), nanosleep() has the following
advantages: it provides a higher resolution for specifying the sleep
interval; POSIX.1 explicitly specifies that it does not interact with
signals; and it makes the task of resuming a sleep that has been
interrupted by a signal handler easier.

答案 2 :(得分:0)

没有办法保证10ms的循环时间。 所有睡眠功能至少睡眠所需时间。 对于便携式解决方案,请使用std::this_thread::sleep_for

#include <iostream>
#include <chrono>
#include <thread>

int main()
{
    for (;;) {
        auto start = std::chrono::high_resolution_clock::now();
        std::this_thread::sleep_for(std::chrono::milliseconds{10});
        auto end = std::chrono::high_resolution_clock::now();
        std::chrono::duration<double, std::milli> elapsed = end-start;
        std::cout << "Waited " << elapsed.count() << " ms\n";
    }
}

根据您要执行的操作,查看Howard Hinnants日期库。

相关问题