Minute Delay Loop

This is a discussion on Minute Delay Loop within the C++ Programming forums, part of the General Programming Boards category; I'm needing minutes to always be 00 seconds. But after running my code for a few minutes, it jumps to ...

time only has second accuracy. You might try clock() or GetTickCount() which accuracy of some milliseconds.
The basic principle is then simply to try to compensate for "oversleep."
For example, take clock() before, Sleep for n milliseconds.
Take clock() again and calculate how many milliseconds it "overslept."
Then subtract that from the amount to sleep next time, and so on.
It will you an average of n millisecond sleep each time, but not perfect.

Basically round current time to last time second was 00, add 1 minute, gives the time you should sleep until.

Then, you can just sleep the time between the calculated time and current time.

This should give you 00 most of the time. Unfortunately, since Windows is not a real-time system (meaning it doesn't make any timing guarantees), theoretically, if it's very busy, it can decide to not schedule (run) your process for as long as it wants.

If timing is VERY critical (eg. in a life support device, or vehicle computer), a real time system should be used, but that is a lot of hassle, and may not be practical. There is a kernel patch that makes Linux real time, but I'm not aware of any real time variant of Windows. Another option is to just accept that your code will work 99.99% of the time, and make sure nothing catastrophic will happen if it oversleeps.

Do you mean that I should expect random increased Sleep millis each cycle?

The only guarantee Sleep(ms) gives you is that, your thread/process will be suspended for AT LEAST ms. Theoretically, Windows can suspend your process for 2 years even if you only asked for 2 seconds. Not likely, but Windows doesn't guarantee it won't do that.