Re: While-Statement
- From: "Eugene Gershnik" <gershnik@xxxxxxxxxxx>
- Date: Tue, 31 Jan 2006 11:11:41 -0800
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
.
- Prev by Date: Re: initialize array within a class by a class member
- Next by Date: Re: While-Statement
- Previous by thread: Re: While-Statement
- Index(es):
Relevant Pages
|
|