0

Let's say I want to run a function when certain conditions are met, for example I want to show a pop up message at a certain hour. Do I have to keep checking every so often or is there any other way to schedule an event without having to have a constant loop? The language doesn't matter, I'd just like to know the algorithm if it exists.

Paskky
  • 11
  • 1
  • 2

1 Answers1

0

Yes, timers exist, you don't need to busy-wait checking the time.

Any OS worthy of the name has sleep functions (sometimes by that name) to sleep for a fixed number of seconds, and/or alarm functions that take a wake-up time. Timers are or can be hardware-supported so the OS isn't busy-waiting either.

POSIX alarm(2) takes a time in seconds, not a time-of-day, but unlike POSIX sleep(2) it can't "wake up early". So you could set an alarm and then use sleep until the alarm wakes you up. Or do other things before the SIGALRM.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847