Created
July 19, 2020 13:34
-
-
Save larytet/e12ee36faf11394476910a6bde1ea717 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Based on https://www.xaprb.com/blog/2006/05/04/how-to-make-a-program-choose-an-optimal-polling-interval/ | |
wait = 128; | |
minWait = 1; | |
maxWait = 32768; | |
didSomething = false; | |
while (true) { | |
didSomething = false; | |
// Do stuff till done | |
do { | |
stuffToDo = pollResource(); | |
if (stuffToDo) { | |
doStuff(stuffToDo); | |
didSomething = true; | |
} | |
} while (stuffToDo); | |
// Decide how long to wait | |
if (didSomething) { | |
wait = max(minWait, wait / 2); | |
} | |
else { | |
wait = min(maxWait, wait * 2); | |
} | |
// Wait | |
sleep(wait); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment