[SDL] How to get constant framerates without busywaits ...

David Olofson david.olofson at reologica.se
Thu Mar 14 11:14:01 PST 2002


On Thursday 14 March 2002 17:49, Stephen Anthony wrote:
> I forgot to mention in the previous email.  I also tried nanosleep, but
> that didn't work either.
>
> Also, the actual code takes much less than 8 ms to execute (more like
> 1-2 ms).  The problem is that I can't wait on 1 ms intervals, only 10
> ms intervals.  So the emulator runs at 50 fps.  If I try a higher fps,
> then it stays running at 50fps, until I reach some crossover point in
> the time it takes to render a frame, in which case it jumps to 100 fps.

How about "busy-waiting" with a sched_yield() in the loop? Although it's 
not as efficient as blocking on a retrace IRQ (you can always dream, 
can't you?) or something, but it kind of allows you to busy wait without 
hogging the CPU, in the eyes of the scheduler. (That is, the scheduler is 
less likely to consider you a CPU hog and lower your priority, as it will 
if you're just busy-waiting.)

BTW, sched_yield() is a pthreads calls. Don't know what it's called on 
non-posix platforms, but I'd be surprized to see a multitasking OS that 
doesn't have a corresponding call. (What it does is basically "call" the 
scheduler to see if any other thread has some work to do. If you're the 
only runnable thread, the call just returns.)


> The main problem is like I stated above: I need 1ms resolution, not 10
> ms, or alternatively, a better algorithm :)

*I* think you need a different design... ;-)


//David Olofson --- Programmer, Reologica Instruments AB

.- M A I A -------------------------------------------------.
|      Multimedia Application Integration Architecture      |
| A Free/Open Source Plugin API for Professional Multimedia |
`----------------------------> http://www.linuxdj.com/maia -'
.- David Olofson -------------------------------------------.
| Audio Hacker - Open Source Advocate - Singer - Songwriter |
`-------------------------------------> http://olofson.net -'




More information about the SDL mailing list