by londons_explore · 5y ago · 0 comments
Remember... your CPU can halt at any time for any number of milliseconds. That means simple things like:
upperBound, lowerBound = readTime()
if (upperBound<deadline)
do_stuff(x, y, z)
Are incorrect... There is no guarantee that the 'if' statement didn't take many milliseconds, and that the stuff didn't end up happening after the deadline.It's also very easy to write code that works, but is theoretically wrong. You will leave a hidden bug that may only rear its head years down the line.
edit: well, after reading Spivak's comment[1] a bit, I guess it does provide strict upper bounds. May be useful to reduce how often you need to use fallback behaviors / get more byzantine. Though I'm not yet sure how to turn that into something useful. No doubt there are some though.
lowerBound, upperBound = readTime()
often without noticing my error. def must_complete_before(func, deadline):
result = func()
lower, upper = time.now()
if upper < deadline:
return result
else:
throw MissedDeadlineException(“Can’t guarantee func completed before deadline”)This API is to determine if something that happened at a given instant is definitely in the past. That’s it!
The APIs provided by TrueTime and Time Sync are useful to compare two events, each with their own uncertainty intervals. Then you can be sure if any event "happened before" the other, or if they're concurrent.
syncTime()
And they take care of the mess