Hi,
yeah, on some OS/cpu combinations threads get starved if they are too
busy. This is why you should try and add an SDL_Delay in your main
loop too.
Below is an “accurate_delay()” function that is used in
pygame.time.Clock.tick_busy_loop(). Why the OS does not stall the
thread is that it uses a combination of a real OS sleep, and a busy
loop at the end to get the accurate sleeping. It uses knowledge of
how bad the clock sleep accuracy on each OS is at sleeping to figure
out how long to sleep for.
So for the right amount of delay time this get’s the benefit of a pure
busy loop on an OS, and does not use 100% cpu. It seems to work ok in
many places… except on some linux 2.4 kernels it used 100% cpu
unfortunately.
#define WORST_CLOCK_ACCURACY 12
static int
accurate_delay (int ticks)
{
int funcstart, delay;
if (ticks <= 0)
return 0;
if (!SDL_WasInit (SDL_INIT_TIMER))
{
if (SDL_InitSubSystem (SDL_INIT_TIMER))
{
RAISE (PyExc_SDLError, SDL_GetError ());
return -1;
}
}
funcstart = SDL_GetTicks ();
if (ticks >= WORST_CLOCK_ACCURACY)
{
delay = (ticks - 2) - (ticks % WORST_CLOCK_ACCURACY);
if (delay >= WORST_CLOCK_ACCURACY)
{
Py_BEGIN_ALLOW_THREADS;
SDL_Delay (delay);
Py_END_ALLOW_THREADS;
}
}
do
{
delay = ticks - (SDL_GetTicks () - funcstart);
}
while (delay > 0);
return SDL_GetTicks () - funcstart;
}On Wed, Jan 26, 2011 at 3:57 PM, David Olofson wrote:
On Wednesday 26 January 2011, at 16.19.10, Ren? Dudfield <@Rene_Dudfield> wrote:
Hi,
Another option is to do a busy loop until the correct time is up. ?This is
often very accurate… but of course you use 100% of a Core/thread to do
this. ?So it’s a trade off.
…and, by doing that on a regular basis, you’re also asking the OS to allow
all sorts of background processes and stuff to preempt your thread whenever
they need some CPU time. So, instead of getting timing jitter, you get the
occasional extended stall.
So, while it can be a hack/solution in some special cases, it’s not really
something you should rely on, unless you can expect to be the only application
running, and/or there are enough CPUs/cores that you can have at least one to
abuse as much as you like.
–
//David Olofson - Consultant, Developer, Artist, Open Source Advocate
.— Games, examples, libraries, scripting, sound, music, graphics —.
| ? http://consulting.olofson.net ? ? ? ? ?http://olofsonarcade.com ? |
’---------------------------------------------------------------------’
SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org