Busy waiting reliability

Feb 13, 2015 at 4:35am
I noticed on some code used through a circuit board code I'm using that there seems "busy waiting" in code I was given.
for (i = 0; i < SOME_MACRO; i++);
Is a compiler technically allowed to completely optimize this out?
If not optimized, is trial-and-error the only way to tell how long the for loop will run for?
Last edited on Feb 13, 2015 at 4:36am
Feb 13, 2015 at 4:52am
I think it is most likely that the programmer has a good familiarity with low-level assembly and knows the specifications of the hardware s/he is coding for, so they could have calculated it. Though less stellar answers are applicable too - it could be trial and error, it could be that a timing functionality exists but the programmer doesn't know about it, or it could even be a speed-up loop:
http://thedailywtf.com/articles/The-Speedup-Loop
Feb 13, 2015 at 7:32am
As LB said, as it is for specific controller, it is probably uses some intrinsic knowledge about it to calculate needed amount of iterations (by knowing controller frequency).
To avoid optimisating loop away completely, value in SOME_MACRO migh be volatile.
Feb 13, 2015 at 4:45pm
Funny link LB, but yes I think it's the former - the code is written for specific hardware so it runs at the right frequency. Thanks for the responses, didn't know about volatile.
Last edited on Feb 13, 2015 at 4:45pm
Topic archived. No new replies allowed.