3 ms·
Sleeping a thread doesn't "choose" when something happens, the programmer does. Sleep functions are just abstractions over hardware timer interrupts. There's so
by sgtnoodle 4y ago
Sleeping a thread doesn't "choose" when something happens, the programmer does. Sleep functions are just abstractions over hardware timer interrupts. There's some inherent complexity there, and abstracting it away is naturally going to impose some limits or compromises. Windows' "sleep" function seems to be especially limited, but being able to precisely schedule a CPU thread to a hardware timer is important for solving a lot of interesting problems. As you push a program to its limits, of course you'll need to carefully choose the abstractions that most optimally achieve your requirements given the available hardware. I agree that bare sleep style functions rarely make sense. Blocking select-style calls with a timeout often do, though, which are largely equivalent to sleep.
The whole point of a CPU is to be general purpose. A CPU isn't optimal for anything. The magic of a computer is that it makes "anything" possible. Why would you want to ban sleep? If somebody makes a crappy arcade game with terrible latency and jitter, good for them. Someone not yet familiar with the nuances of "sleep" will learn a lot faster building some crappy but functional programs than they will trying to learn a modern graphics API. A seasoned expert will readily barf out a crappy program with some sleep calls in it when they know it's good enough to solve the problem at hand.