title: Replacing usleep with nanosleep
body: <p>I want to replace obsolete usleep function with nanosleep in my code:</p>
  
  <pre><code>static int timediff( struct timeval *large, struct timeval *small )
  {
          return (   ( ( large-&gt;tv_sec * 1000 * 1000 ) + large-&gt;tv_usec )
                   - ( ( small-&gt;tv_sec * 1000 * 1000 ) + small-&gt;tv_usec ) );
  }
  
  struct performance_s
  {
          struct timeval acquired_input;
  };
  
  performance_t *performance_new( int fieldtimeus )
  {
       performance_t *perf = malloc( sizeof( performance_t ) );
       if( !perf ) return 0;
  
       gettimeofday( &amp;perf-&gt;acquired_input, 0 );
  
       return perf;
  }
  
  performance_t *perf = 0;
  
  int performance_get_usecs_since_frame_acquired( performance_t *perf )
  {
      struct timeval now;
      gettimeofday( &amp;now, 0 );
      return timediff( &amp;now, &amp;perf-&gt;acquired_input );
  }
  
  
  int fieldtime = videoinput_get_time_per_field( norm );
  
  
  if( rtctimer ) {
      while( performance_get_usecs_since_frame_acquired( perf )
                    &lt; ( (fieldtime*2) - (rtctimer_get_usecs( rtctimer ) / 2) ) ) {
          rtctimer_next_tick( rtctimer );
      }
  } else {
      int timeleft = performance_get_usecs_since_frame_acquired( perf );
      if( timeleft &lt; fieldtime )
          usleep( fieldtime - timeleft );
  </code></pre>
  
  <p>Questions: does this replacement get the same precision timing than with usleep ( and is it a correct replacement )?</p>
  
  <pre><code>struct timespec delay = {0, ( fieldtime - timeleft )}; nanosleep(&amp;delay, NULL);
  </code></pre>