1

I have a for loop inside a while (1) and I want the loop to average approx 2 minutes through each entire for loop cycle. This requires sleeping between each iteration for a different time based on execution time of the iteration and time remaining in my 2 minute shot clock. Any ideas.

I was moving towards something

while(true)
{
   unsigned long begin_loop = OCX::micro_stamp();
   for (int i = 0; i < _vector.size(); i++) 
    {
        unsigned long start = OCX::micro_stamp();
        unsigned long avg = ( ( begin_loop - start) / ( i + 1 ) );
        usleep ( ( 120000000 - ( (_vector.size() - i) * avg ) ) / ( _vector.size() - i ) );
        try
        {



        }
        catch {
        }
   }
}
leppie
  • 115,091
  • 17
  • 196
  • 297
sjohnson
  • 69
  • 2
  • 13

2 Answers2

0

At the end of each iteration, sleep for ((120 - total elapsed time) / (vector.size() - i))

jmatula
  • 1
  • 1
0

At the end of each iteration, sleep for

((120000000 - total elapsed microseconds) / (vector.size() - i))
jmatula
  • 38
  • 4