I am trying to do short delays (e.g. 400 Microseconds). I used my own busy-waiting function and Linux nanosleep. However, the problem is that the delay time varies from a call to another. For the example, the following code takes different times to execute ranges from 187 usec to 300 usec:
gettimeofday(&tvBegin, NULL); // begin
for (double d = 0; d < 50000; d++); // busy waiting
gettimeofday(&tvEnd, NULL); //end
I understand the process may be preempted during the busy wait. One of the solutions I tried is to set the process priority to -1 since all of my other processes are set to 0. But, it didn't work.
How can I get my busy wait code to take the same time to execute?
Any feedback is highly appreciated.