C++ 是否可以创建一个跳过循环中某个函数的计时器?

C++ 是否可以创建一个跳过循环中某个函数的计时器?,c++,timer,chrono,chronometer,C++,Timer,Chrono,Chronometer,在我的项目中,我使用opencv捕捉网络摄像头的帧,并通过一些函数检测其中的一些内容。问题是,在一个确定函数中,不一定要捕获所有帧,例如每0.5秒获取一帧就足够了,如果时间尚未结束,循环将继续到下一个函数。代码中的想法是: while(true){ //read(frame) //cvtColor(....) // and other things time = 0;// start time if (time == 0.5){ determinatefuncti

在我的项目中,我使用opencv捕捉网络摄像头的帧,并通过一些函数检测其中的一些内容。问题是,在一个确定函数中,不一定要捕获所有帧,例如每0.5秒获取一帧就足够了,如果时间尚未结束,循环将继续到下一个函数。代码中的想法是:

while(true){
  //read(frame)
  //cvtColor(....)
  // and other things
  time = 0;// start time
  if (time == 0.5){
      determinatefunction(frame, ...)
  }else {
      continue;
  }
  //some others functions
}
我尝试在chrono库中执行与上述类似的操作:

// steady_clock example
#include <iostream>
#include <ctime>
#include <ratio>
#include <chrono>

using namespace std;

void foo(){
cout << "printing out 1000 stars...\n";
  for (int i=0; i<1000; ++i) cout << "*";
  cout << endl;
}

int main ()
{
    using namespace std::chrono;

    steady_clock::time_point t1 = steady_clock::now();
    int i = 0;
    while(i <= 100){
        cout << "Principio del bucle" << endl;
        steady_clock::time_point t2 = steady_clock::now();
        duration<double> time_span = duration_cast<duration<double>>(t2 - t1);
        cout << time_span.count() << endl;
        if (time_span.count() == 0.1){
            foo();
            steady_clock::time_point t1 = steady_clock::now();
        }else {
            continue;
        }
        cout << "fin del bucle" << endl;
        i++;
    }
}
//稳定时钟示例
#包括
#包括
#包括
#包括
使用名称空间std;
void foo(){

cout将
==
与浮点计算结合使用,在大多数情况下是错误的

当差值正好为
0.1
时,不能保证执行
duration\u cast(t2-t1)

相反,它可能类似于
0.099324
,并且在下一次迭代中
0.1000121

使用
=
,如果
没有多大意义,则在
中定义另一个
t1

if (time_span.count() >= 0.1) {
  foo();
  t1 = steady_clock::now();
}

==
与浮点计算结合使用通常是错误的

当差值正好为
0.1
时,不能保证执行
duration\u cast(t2-t1)

相反,它可能类似于
0.099324
,并且在下一次迭代中
0.1000121

使用
=
,如果
没有多大意义,则在
中定义另一个
t1

if (time_span.count() >= 0.1) {
  foo();
  t1 = steady_clock::now();
}