title: how to define a loop to be run for some seconds/minutes long
body: <p>My purpose is to execute a <code>while</code> loop for a defined time (e.g. 90 seconds for this example). It does not have to be exactly 90 s, but 1-2 second inaccuracy is acceptable. I trued to use clock()` function for this purpose:</p>
  
  <pre><code>int main(void){
        clock_t start, end;
        volatile double elapsed;
        start = clock();
        int terminate = 1;
        while(terminate)
        {
                end = clock();
                elapsed = ((double) (end-start)) / (double) CLOCKS_PER_SEC *1000;
                printf("elapsed time:%f\n",elapsed);
                if(elapsed &gt;= 90.0)
                          terminate = 0;
                 usleep(50000);
         }
        printf("done..\n");
      return 0;
  }
  </code></pre>
  
  <p>when I run it on my laptop (x86, 3.13 kernel, gcc 4.8.2), my stopwatch measures <strong>72 seconds</strong> for it to be completed. (1000 was necessary to have the <code>elapsed</code> in seconds accuracy on my laptop)</p>
  
  <p>When I run it on an ARM device (armv5tejl, 3.12 kernel, gcc 4.6.3) it takes <strong>58 seconds</strong> to complete the code. (I needed to used <code>100</code> on <code>elapsed</code> on the armv5). </p>
  
  <p>I run the code on a room temperature, so the clock should be stable. I know that kernel sleeps the threads and has inaccuracy with time to wake them up, etc. Therefore, as I said previously I don't expect to get a perfect timing, but it should have some accuracy. </p>
  
  <p>I had tried to use only <code>usleep</code> (even <code>nanosleep</code>) but the resolution was not good as well. At the end I come up with the the bottom code that fetches the system time (hour, minute, second) then calculate the elapsed time. And it works with a good accuracy. </p>
  
  <p>I wonder if there is another solution that would be less costly to use? </p>
  
  <pre><code>typedef struct{
      int hour;
      int minute;
      int second;
  } timeInfo;
  
  timeInfo getTimeInfo(void){
      timeInfo value2return;
      time_t rawtime;
      struct tm * timeinfo;
      time(&amp;rawtime);
      timeinfo = localtime(&amp;rawtime);
      value2return.hour = timeinfo-&gt;tm_hour;
      value2return.minute = timeinfo-&gt;tm_min;
      value2return.second = timeinfo-&gt;tm_sec;
      return value2return;
  }
  
  int checkElapsedTime(const timeInfo *Start, const timeInfo *Stop, const int Reference){
      if(Stop-&gt;hour &lt; Start-&gt;hour){
          printf("1:%d\n", (Stop-&gt;hour +24) *3600 + Stop-&gt;minute *60 + Stop-&gt;second - (Start-&gt;hour *3600 +Start-&gt;minute * 60 + Start-&gt;second));
           if( ( (Stop-&gt;hour +24) *3600 + Stop-&gt;minute *60 + Stop-&gt;second - (Start-&gt;hour *3600 +Start-&gt;minute * 60 + Start-&gt;second)) &gt;= Reference )
              return 0; //while(0): terminate the loop
           else
               return 1; //while(1)
      }else{
          printf("2:%d\n",Stop-&gt;hour *3600 + Stop-&gt;minute *60 + Stop-&gt;second - (Start-&gt;hour *3600 +Start-&gt;minute * 60 + Start-&gt;second));
          if( (Stop-&gt;hour *3600 + Stop-&gt;minute *60 + Stop-&gt;second - (Start-&gt;hour *3600 +Start-&gt;minute * 60 + Start-&gt;second)) &gt;= Reference )
              return 0;
          else
              return 1;
      }
  }
  
  int main(void){
  
      timeInfo stop, start = getTimeInfo();
      int terminate = 1;
      while(terminate)
      {
          stop = getTimeInfo();
          terminate = checkElapsedTime(&amp;start, &amp;stop, 90);
          usleep(5000); //to decrease the CPU load
      }
  
      printf("terminated\n");
  
      return 0;
  }
  </code></pre>
  
  <p>Lastly, I need to run it inside a <code>pthread</code>.</p>