Hello,
I would like to put my app to sleep for 100 microseconds. I've used usleep function but I've noticed that it sleeps for about 200 microseconds. I wrote the following program to check what's what:
#include <stdio.h> #include <time.h> #include <unistd.h> int main(int argc, char **argv) { struct timespec res, tp1, tp2; clock_getres(CLOCK_MONOTONIC, &res); clock_gettime(CLOCK_MONOTONIC, &tp1); usleep(100); clock_gettime(CLOCK_MONOTONIC, &tp2); printf("resolution: %ld sec %ld ns\n", res.tv_sec, res.tv_nsec); printf("time1: %ld / %ld\n", tp1.tv_sec, tp1.tv_nsec); printf("time2: %ld / %ld\n", tp2.tv_sec, tp2.tv_nsec); printf("diff: %ld ns\n", (tp2.tv_sec - tp1.tv_sec) * 1000000000 + tp2.tv_nsec - tp1.tv_nsec); }
The results are:
resolution: 0 sec 1 ns
time1: 136 / 905181953
time2: 136 / 905369318
187365 ns
So I get additional 87 microseconds. Is it possible to improve this and get as close to 100 us sleep as possible? Do I have to change something in my Linux configuration and recompile it?