Skip to content

Interrupts vs polling ​

Polling checks for data repeatedly. Interrupts react when a device signals that data is available.

MechanismBest useTrade-off
Pollingsimple tests, predictable short waitswastes CPU when events are rare
Interruptsnormal interactive inputneeds event-loop structure

Keyboard events are sporadic, so interrupts usually fit better. Polling is still useful for understanding the controller and for small deterministic experiments. Both versions exercise the same controller rules; only the decision about when to ask for data changes.

Timeout discipline ​

Polling loops need limits. A loop that waits forever for a key can hang a test, a demo, or a grading run.

Timer-driven limits make behavior observable. "Wait until ESC is released or until N seconds of idle time pass" is precise and testable. "Wait for some input" can easily become impossible to test.

TIP

Poll with a clear maximum number of attempts or a timer-driven timeout, then return an error the caller can handle.

Course/docs licensed CC BY 4.0. Code licensed MIT.