Re: Why INFINITE loop in a thread occupy so much CPU time??

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



On Sun, 18 Nov 2007 05:24:06 +1000, "Ian Semmel"
<anyone@xxxxxxxxxxxxxxxxx> wrote:




On Wed, 14 Nov 2007 22:16:44 -0800 (PST), lostlander
<lostlander.tom@xxxxxxxxx> wrote:

I've got a thread:

threadFunc(LPVOID pdata)
{
while(1)......
}

why it takes so much cpu time on windows??

What did you expect to happen? Threads that don't yield consume as
many
CPU
cycles as the OS will give them, subject to things like relative
priority,
dynamic priority adjustment, and so forth.

--
Doug Harrison
Visual C++ MVP

This is really a Windows thing rather than a programming problem.

There is nothing intrinsically wrong with writing CPU-intensive
functions. Operating Systems exist (and have existed for decades) that
handle them OK. They should just be running at the lowest priority.

Functions such as Yield, which force a programmer to usurp the role of
the operating system are not a solution in my opinion.

The not uncommon situation in Windows whereby one rogue program can
screw up the entire system is far from satisfactory.

It depends on what you call "CPU Utilization" too. Windows regards the
Idle process as "idle CPU" and any process that is not the "idle task"
is "utilized CPU" so 100% CPU utilization on Windows mostly just means
there is a task running in the system that is not the idle task and
that task is not "yielding" to the scheduler and it is being preempted
and immediately rescheduled and dispatched. The idle task in Windows
is the task responsible for zeroing free pages and I believe once it
has done that it actually halts the processor(s). See Intel's comments
on HLT and documentation on power control for mobile processors. One
cannot execute HLT in user-land.

The empty loop: for(;;); assembles to:

here: jmp short here

and this will generate 100% cpu utilization but won't degrade the
system at all. On a P4 system it actually generates 50% since the HT
processor is running idle task in alternation with the looping task.

System degradation doesn't seem to occur in Windows until you are
overburdening the kernel or GUI threads.

Yield is obsolete and generates no code. Neither does it remedy CPU
utilization.

Even using SetPriorityClass to set the application to
IDLE_PRIORITY_CLASS will only make it equal in priority to the idle
task and cause apparent CPU utilization. Messing with process or
thread priorities is generally a bad idea since priority inversion or
deadlock can occur if you are not careful.

MSDN states Sleep(milliseconds) is the preferred method for
applications that don't create windows to yield time back to the
system. They also state that Sleep(0) is the method. This appears to
be wrong. Sleep(1) on Windows XP will cause low CPU utilization and
appears to yield back to idle for the remainder of the scheduler
interval. Sleep(0) still runs the thread and causes high CPU
utilization.

The fact is, the simplest method for writing an infinite loop with low
CPU utilization under Windows is:

int main(void)
{

for(;;)
Sleep(1);
return 0;
}


In summary, if you MUST poll or write infinite loops, call Sleep(1) to
yield back to idle and keep CPU utilization under control. If you can
afford to sleep longer, do it. Otherwise if your process creates
windows use WaitForSingleObject or MsgWaitForMultipleObjects.

.



Relevant Pages

  • Re: Why INFINITE loop in a thread occupy so much CPU time??
    ... dynamic priority adjustment, and so forth. ... This is really a Windows thing rather than a programming problem. ... It depends on what you call "CPU Utilization" too. ...
    (microsoft.public.vc.mfc)
  • In the midst of chaos, an adventure can be found Topic Summary: A beginners journey into the realm o
    ... I've built it up and am installing windows. ... AMD CPU Driver & Optimizer. ... Quick restart then the mobo drivers, another restart and on go the ... I install ASUS PC Probe and the AI Booster utilities. ...
    (comp.programming)
  • Re: 2003 Server slowed to a crawl
    ... click Processes then CPU column to sort by ... Investigate a possible Network problem (is there a broadcast storm ... >> method for telling the computer's processor that it needs attention. ... >> which have signed drivers (such hardware is sold with a Microsoft Windows ...
    (microsoft.public.windows.server.general)
  • Re: 2003 Server slowed to a crawl
    ... Check system uptime (pagefile.sys modified date or in Task Manager: CPU ... Remove unnecessary network protocols (NWLink IPX/SPX is often at fault ... which have signed drivers (such hardware is sold with a Microsoft Windows ...
    (microsoft.public.windows.server.general)
  • Re: Starting Windows 2000 hangs on new install.
    ... Windows Me is happy with either. ... CPU temperature ... If OK reboot from win2k installation disk (you may have to ...
    (microsoft.public.win2000.setup)