To let the thread to suspend the exact amount of time, without being affected by thread scheduling, we can use nanospin().
int nanospin( const struct timespec *when );
The nanospin() function occupies the CPU for the amount of time specified by the argument when without blocking the calling thread. (The thread isn’t taken off the ready list.) The function is essentially a do…while loop.
The first time you call nanospin(), the C library invokes nanospin_calibrate() with an argument of 0 (interrupts enabled), if you haven’t already called it.
int nanospin_ns( unsigned long nsec );
The nanospin_ns() function busy-waits for the number of nanoseconds specified in nsec, without blocking the calling thread.
void nanospin_count( unsigned long count );
The nanospin_count() function busy-waits for the number of iterations specified in count. Use nanospin_ns_to_count() to turn a number of nanoseconds into an iteration count suitable for nanospin_count().