Re: While-Statement



Saul775 wrote:
> Just a quick question I'd like to ask as I've always wondered the
> answer to this.
>
> Suppose you have the following code...
>
> while (1)

This line will usually trigger a compiler warning (constant loop condition)
on high enough warning level. The canonical form VC forces for 'forever'
loop is IIRC

for ( ; ; )


> {
> // Some code in here that doesn't have WaitForSingleObject()
> // Maybe something really simple... x = 1;
> }
>
> Why is it that this code will consume 99% of the CPU when looking at
> it through task manager? However, I have other tasks running that
> consume approximately 0-1% or a fraction of the CPU power that are
> actually monitoring stuff. How do they incorporate an infinite loop
> that consumes very little CPU power? Is it possible to emulate their
> infinite loop?

They wait for some event. The normal form for any 'forever' loop is
something like this

for( ; ; )
{
result = WaitFor(<some waitable thing(s)>, INFINITE);
if (result == break condition)
break;
//do whatever you need to do
}

What WaitFor really stands for obviously depends on thing you want to wait.
The important thing is that when this function is entered the thread goes to
be inactive in kernel mode and not consume any CPU time untill 'wake-up'
condition happens.


> I've tried the following before...
>
> while (1)
> {
> // Statements
> Sleep(1000);
> }
>
> But I know that's probably not the best approach.

Bad as explained in another post.


--
Eugene
http://www.gershnik.com


.



Relevant Pages

  • Re: While-Statement
    ... > Why is it that this code will consume 99% of the CPU when looking at it ... > approximately 0-1% or a fraction of the CPU power that are actually ... How do they incorporate an infinite loop that consumes ... socket I/O completion, file I/O completion, process completion, ...
    (microsoft.public.vc.language)
  • Re: Changing process priority
    ... How do I make sure a simple whileloop ... doesn't consume half my CPU? ... Though sometimes there is a "idle process" that runs ... an infinite loop consuming all leftover CPU time, ...
    (comp.lang.c)
  • Re: Is it possible to implement UART by GPIO?
    ... but it will consume a lot of cpu power. ... "chanter" wrote in message ... > let GPIO high or low by timer interrupt to emulate time sequence of UART ...
    (microsoft.public.windowsce.platbuilder)
  • Mediaplayer consumes aaaall of my CPU! HELP
    ... normally because Windows media player 10 have started to consume 90% ... of the CPU power according to the task manager. ... How do I get rid of this? ...
    (microsoft.public.windowsmedia)
  • Re: Problems with Windows Media Encoder. WMEncoder RunState dosnt changes.
    ... This is a very thight loop which will consume most of your ... CPU power and will not dispatch messages. ... least sleep 200-500 millisecs at each iteration. ...
    (microsoft.public.windowsmedia.sdk)