How to avoid using up cpu power?

Hi,
here is an urgent question.
How to avoid the program occupying all the cpu computing power
when waiting for events? I used SDL_WaitEvent.but seemed that no difference
with do{SDL_pollEvent())while(true);

wish getting your help as soon as possible . GREAT Thanks in advance.

yours roanldz.
4/23.

with do{SDL_pollEvent())while(true);

SDL_Event event;

while (1)
{
	SDL_PollEvent(&event);
	SDL_Delay(1);
}

…that will do it. The SDL_Delay(1) will pause until the first time it
can get control back after sleeping for x milliseconds, so specifying 1
millisecond will wait for at least one timeslice, which, while much more
than one millisecond, will suit your needs.

–ryan.

There is a SDL_WaitEvent which might be more efficient than this.On Sun, 22 Apr 2001, Ryan C. Gordon wrote:

with do{SDL_pollEvent())while(true);

SDL_Event event;

while (1)
{
SDL_PollEvent(&event);
SDL_Delay(1);
}

…that will do it. The SDL_Delay(1) will pause until the first time it
can get control back after sleeping for x milliseconds, so specifying 1
millisecond will wait for at least one timeslice, which, while much more
than one millisecond, will suit your needs.

Hi,
here is an urgent question.
How to avoid the program occupying all the cpu computing power
when waiting for events? I used SDL_WaitEvent.but seemed that no difference
with do{SDL_pollEvent())while(true);

In either case, your loop is probably running quite tightly. In the case of
the blocking function (WaitEvent, right? - it’s been a while ;^) ),
it’s probably not blocking for very long… it’s probably getting constant
mouse motion events. :wink:

When in doubt, stick an SDL_Delay() in to slow the loop down - it reduces
CPU usage.

-bill!

Heh, take a look at the source sometime.On Sun, Apr 22, 2001 at 03:14:37PM -0700, Lawrence W. Leung wrote:

There is a SDL_WaitEvent which might be more efficient than this.

On Sun, 22 Apr 2001, Ryan C. Gordon wrote:

with do{SDL_pollEvent())while(true);

SDL_Event event;

while (1)
{
SDL_PollEvent(&event);
SDL_Delay(1);
}


Martin

Bother! said Pooh, and twitted Roger Barnes.