It is well known that a busy waiting is bad because it wastes CPU time. But what is done in modern operating systems to make the OS idle thread work well? Is it just a busy wait? If so, what is done to conserve power? What is the best approach?
Is it possible to make a busy wait that acts like an idle thread, instead of doing something like thread_yield() inside of a tight loop?
This is a really broad question, so there's no way to give you a very specific answer; I'll give you some general guidelines.
In general, in a modern program you are likely to use some type of a thread pool library to manage threads for you. These often have some type of idle_timeout after which a thread will be closed; if they need to add some threads, they will spin up new ones.
If you are managing your own thread lifecycle, there are a number of approaches you could take. Perhaps something like waiting on an event or signal might work in your case. In such a case, the thread that is assigning work could raise an event to wake the thread up. If that seems too complicated, something as simple as sleeping for some amount of time would be better than doing a check in a loop.
Depending on your situation, some other approach might make sense. But in general, you definitely want to try to utilize libraries that have already been written to handle the situation, rather than trying to write one from scratch.
Ah thanks for the clarifying comment. It looks like there's some interesting information in here http://en.wikipedia.org/wiki/System_Idle_Process about that topic. If that information is accurate sounds like it's O/S specific; some windows versions just call halt instructions in a loop, while later ones call hardware specific instructions to cause clock speed reductions or other power-saving measures.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With