在C ++中经过30ms的时间后,退出循环的最好方法是什么 [英] What is the best way to exit out of a loop after an elapsed time of 30ms in C++

查看:201
本文介绍了在C ++中经过30ms的时间后,退出循环的最好方法是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++中,尽可能接近30ms的退出循环的最佳方法是什么?轮询提升:microsec_clock?轮询QTime?还有什么?

What is the best way to exit out of a loop as close to 30ms as possible in C++. Polling boost:microsec_clock ? Polling QTime ? Something else?

像:

A = now;
for (blah; blah; blah) {
    Blah();
    if (now - A > 30000)
         break;
}

它可以在Linux,OS X和Windows上使用。

It should work on Linux, OS X, and Windows.

循环中的计算用于更新模拟。每30秒,我想更新视口。

The calculations in the loop are for updating a simulation. Every 30ms, I'd like to update the viewport.

推荐答案

此链接中的代码段示例几乎是您想要的:

The code snippet example in this link pretty much does what you want:

http:// www.cplusplus.com/reference/clibrary/ctime/clock/

改编自他们的例子:

void runwait ( int seconds )
{
   clock_t endwait;
   endwait = clock () + seconds * CLOCKS_PER_SEC ;
   while (clock() < endwait)
   {
      /* Do stuff while waiting */
   }
}

这篇关于在C ++中经过30ms的时间后,退出循环的最好方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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