linux - What is the best way to get tight timing of a thread in c (pthreads) -
ok have thread need run every 10ms, takes variable amount of processing time (for simplicity can assume processing time less 10ms). small deviations in timing will, on time, add , become problem.
this current solution. seems clunky, moreso i'm worried time takes run timeval_subtract causing timing off. have better solution?
this library , cannot use system resources timers or clocks.
void mythread(void *ptr ) { struct timeval tv_being, tv_end; int ustosleep; while(1) { gettimeofday(&tv_begin) //do stuff //now determine how long sleep wake 10ms after previous wakeup gettimeofday(&tv_end) ustosleep = timeval_subtract(tv_begin, tv_end); //this return 10ms minus elapsed time usleep(ustosleep); } return; }
you're @ mercy of granularity of process scheduler you're using. 10ms achievable remember when sleeping operating system not schedule when available woken up. if other processes ahead of may chosen run instead may delayed.
your approach (or good) of approximation can get.
if require better scheduling can compiling linux kernel real time options enabled give finer scheduling granularity.
Comments
Post a Comment