CPU Usage and Win32

This is a discussion on CPU Usage and Win32 within the C++ Programming forums, part of the General Programming Boards category; It seems whenever I run/make a win32 program, it always uses up 100% of the cpu, at least thats what ...

That happens because of your message loop. If you switch PeekMessage() with GetMessage(), then your CPU usage will drop back to normal levels.. although if you want stuff to happen during the time when there are no messages available, this might not be possible. Alternately, a quick-fix solution is to add a Sleep(1); line to your message loop. I believe this tells the OS basically to 'give up the rest of the CPU time slice', if I remember correctly from wherever I read it. But the real reason it works might also just be because the time spent sleeping (1ms) is hugely greater than the time spent on processing the rest of the contents of the loop - I'm no expert on such things Also try putting your message loop in a separate thread with idle priority or something.

Hunter2---Sleep(0) gives up the time slice, Sleep(1) waits a millisecond. I believe that is the way it works.

yah, from msdn:

Originally Posted by MSDN

A value of zero causes the thread to relinquish the remainder of its time slice to any other thread of equal priority that is ready to run. If there are no other threads of equal priority ready to run, the function returns immediately, and the thread continues execution.

Sleep(1) is usually used to give up the rest of the time slice as Sleep(0) only yields to threads of equal or higher priority, leaving lower priority threads dead in the water. Sleep(1) will yield to any ready thread.