r/AskComputerScience • • Mar 21 '15

How do CPUs run timers etc without saturating themselves? Where can a layman go to learn a little about this?

28 Upvotes

Okay, this is a dumb question, I know. This is me.

If I write a piece of code that simply says

wait 10 seconds
print("Hello")

and keep an eye on my system stats, I notice that my CPU remains at only 1-2% execution.

This makes intuitive sense, because that's not a complicated task. But what is the CPU actually 'thinking' during that time? I can't really conceive of how it would be anything other than

while(true)
    if it's been 10 seconds
        print("Hello")

a billion times per second, occupying every single instruction and incrementing some counter. That's obviously not what the CPU's doing, so what is it doing? Can the CPU know to wait X period of time before repeating an instruction? What's going on under there?

Related question: could anyone recommend an online lecture series, a book, or some other free/low-cost resource for learning a bit about this kind of thing?

Any input appreciated.