Search This Blog

Wednesday, September 30, 2009

C++, Polling Is Wasting CPU Cycles

Do not do this: (unless you must)

whlie (true) {
//add No blocking function. 
//OR CALL sleep() here

This is called polling, and it will peg your CPU (100% for that core). Try use some system call to awake this process, but not busy loop.

Sometimes, an infinite loop is ok, such as gaming and servers. But usually, they have some blocking function inside the infinite loop. For gaming, it's waiting for input; for an Internet server, it is waiting for a connection (a blocking accept() call).

No comments: