Preventing 100% CPU use polling for events

I’m using SDL for a media player front end that has little to do but react to user input, timer notifications and player state info. Since 2.0, WaitEvent doesn’t wait but just busy loops, so my application uses 100% of a CPU core. Is there an “official” way to deal with this, or am I just to put some sleeps in? The documentation of WaitEvent says helpful “This may change in the future to improve power savings”, but it’s not just power savings that suffers (think hot CPU, fans starting to blow etc.).

JAL

I don’t think it’s super hard to hack in some SDL_cond structures… or not?On 01/16/2014 05:50 PM, jal wrote:

I’m using SDL for a media player front end that has little to do but
react to user input, timer notifications and player state info. Since
2.0, WaitEvent doesn’t wait but just busy loops, so my application
uses 100% of a CPU core. Is there an “official” way to deal with this,
or am I just to put some sleeps in? The documentation of WaitEvent
says helpful “This may change in the future to improve power savings”,
but it’s not just power savings that suffers (think hot CPU, fans
starting to blow etc.).

JAL


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

2014/1/16 jal

I’m using SDL for a media player front end that has little to do but
react to user input, timer notifications and player state info. Since 2.0,
WaitEvent doesn’t wait but just busy loops, so my application uses 100% of
a CPU core. Is there an “official” way to deal with this, or am I just to
put some sleeps in? The documentation of WaitEvent says helpful “This may
change in the future to improve power savings”, but it’s not just power
savings that suffers (think hot CPU, fans starting to blow etc.).

This is not supposed to happen… SDL should sleep while waiting on new
events.
Something isn’t working right on your setup.

jal wrote:

I’m using SDL for a media player front end that has little to do but react to user input, timer notifications and player state info. Since 2.0, WaitEvent doesn’t wait but just busy loops, so my application uses 100% of a CPU core. Is there an “official” way to deal with this, or am I just to put some sleeps in? The documentation of WaitEvent says helpful “This may change in the future to improve power savings”, but it’s not just power savings that suffers (think hot CPU, fans starting to blow etc.).

JAL

SDL_Delay(1) at the end of your main loop should help.

I had the same condition, with SDL2.0 and 2.01.
The use of SDL_Delay(10) was good for me.------------------------
Armando Alaminos Bouza

That doesn’t really help for SDL_WaitEvent(), though…

Jonny DOn Thu, Jan 16, 2014 at 3:24 PM, alabouza wrote:

I had the same condition, with SDL2.0 and 2.01.
The use of SDL_Delay(10) was good for me.


Armando Alaminos Bouza


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

I don’t know how advanced the code is, could we maybe see it, or version of
it we could test?On Jan 16, 2014 2:24 PM, “alabouza” wrote:

I had the same condition, with SDL2.0 and 2.01.
The use of SDL_Delay(10) was good for me.


Armando Alaminos Bouza


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Would it be possible to use SDL_PollEvent and sleep?

Dear Mr. Manard,

I wrote a simple wrapper like this :

int WaitForUserInput(void)
{
int const idle_time = 10;
int ret = 0;
SDL_Event ev;

while (ret == 0)
{
SDL_PumpEvents();
ret = SDL_PeepEvents(&ev, 1, SDL_PEEKEVENT, SDL_FIRSTEVENT , SDL_LASTEVENT );
if (ret)
break;

  SDL_Delay(idle_time );

}
return ret;
}

Ten ms is good for my apps, but can be changed starting from 1 ms.

All applications are “green” again, taking 0-1% CPU when they are in idle state.

R Manard wrote:> I don’t know how advanced the code is, could we maybe see it, or version of it we could test? On Jan 16, 2014 2:24 PM, “alabouza” <@Armando_Alaminos_Bou (@Armando_Alaminos_Bou)> wrote:

   	I had the same condition, with SDL2.0 and 2.01.

The use of SDL_Delay(10) was good for me.

Armando Alaminos Bouza


SDL mailing list
SDL at lists.libsdl.org (SDL at lists.libsdl.org)
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org (http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org)


Armando Alaminos Bouza

Sorry, I was talking about jal at halhurs.com or whatever , the original
question guy. Its always cool to see how someone else codes though. To me
code is like some sort of poetryOn Jan 16, 2014 6:55 PM, “alabouza” wrote:

Dear Mr. Manard,

I wrote a simple wrapper like this :

int WaitForUserInput(void)
{
int const idle_time = 10;
int ret = 0;
SDL_Event ev;

while (ret == 0)
{
SDL_PumpEvents();
ret = SDL_PeepEvents(&ev, 1, SDL_PEEKEVENT, SDL_FIRSTEVENT , SDL_LASTEVENT
);
if (ret)
break;

SDL_Delay(idle_time );
}
return ret;
}

Ten ms is good for my apps, but can be changed starting from 1 ms.

All applications are “green” again, taking 0-1% CPU when they are in idle
state.

R Manard wrote:

I don’t know how advanced the code is, could we maybe see it, or version
of it we could test? On Jan 16, 2014 2:24 PM, “alabouza” <> wrote:

Quote:

I had the same condition, with SDL2.0 and 2.01.
The use of SDL_Delay(10) was good for me.

Armando Alaminos Bouza


SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


Armando Alaminos Bouza


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Jonas Kulla wrote:

This is not supposed to happen… SDL should sleep while waiting on new events.
Something isn’t working right on your setup.

Well, at first I used SDL_PollEvent(), which caused the 100% CPU use. The I consulted the on-line documentation, and found SDL_WaitEvent(), but that mentions:

Wiki (http://wiki.libsdl.org/SDL_WaitEvent) wrote:

As of SDL 2.0, this function does not put the application’s process to sleep waiting for events; it polls for events in a loop internally. This may change in the future to improve power savings.

So I didn’t bother with it. However, today I gave it a try anyway, and it indeed seems to reduce the CPU use, so it seems the documentation is out of date.

JAL

Documentation fixed, thanks!On Fri, Jan 17, 2014 at 3:37 AM, jal wrote:

Jonas Kulla wrote:

This is not supposed to happen… SDL should sleep while waiting on new
events.
Something isn’t working right on your setup.

Well, at first I used SDL_PollEvent(), which caused the 100% CPU use. The
I consulted the on-line documentation, and found SDL_WaitEvent(), but that
mentions:

Wiki http://wiki.libsdl.org/SDL_WaitEvent wrote:

As of SDL 2.0, this function does not put the application’s process to
sleep waiting for events; it polls for events in a loop internally. This
may change in the future to improve power savings.

So I didn’t bother with it. However, today I gave it a try anyway, and it
indeed seems to reduce the CPU use, so it seems the documentation is out of
date.

JAL


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org