[SDL] Strange SDL_Delay behavior
Bob Pendleton
bob at pendleton.com
Thu May 8 09:10:01 PDT 2003
On Wed, 2003-05-07 at 10:35, Serge S. Fukanchik wrote:
> Hi there,
> i'm new to SDL and tried few test programs.
> And i can't explain to myself this odd behavior:
> when i launch this simple program
>
> #include <iostream>
> #include "SDL.h"
>
> int main(int argc, char *argv[])
> {
> if(SDL_Init(SDL_INIT_TIMER) == -1) {
> std::cout << "Can't initialize timer" << std::endl;
> return -1;
> }
> Uint32 max = 1000;
> Uint32 min = 1000;
> for(Uint32 i=0;i<10;++i) {
> Uint32 before, after, delta;
> before = SDL_GetTicks ();
> SDL_Delay(1000);
> after = SDL_GetTicks ();
> delta = after - before;
> if(max < delta) max = delta;
> if(min > delta) min = delta;
> SDL_Quit ();
First, move this statement out side of the loop. You are using SDL
functions after you have shut down SDL. Second, get rid of the extra
call to SDL_GetTicks() each time you call a system function you take a
risk of having the process rescheduled, not to mention adding an unknown
delay to the program.
If we assume that the system clock ticks every 10 milliseconds, and we
assume that your request of a 1000 millisecond delays gets converted
internally to a request for a 100 tick delay. Then the time until the
first tick can be any value t in the range 0 < t < 10. After the first
tick the delay is always 10 milliseconds. So, the time can come up short
by as much as 9.99... milliseconds but on average will be short my 5
milliseconds. Which, BTW, is right on what you sample show.
> }
> std::cout << "min="<<min<<" max="<<max << std::endl;
> }
>
> it's output looks like this:
> min=991 max=1022
> min=995 max=1000
BTW, when doing statistical sampling like this you need a larger sample
size than you generated.
All in all, it looks to me like you are seeing exactly what you should
be seeing.
Bob Pendleton
> ...
>
> I can see why max=1022 here, but i can't see why SDL_Delay returns
> earlier than before + 1000.
> It is acceptable for SDL_Delay to get more time than requested, but
> i can't see any reason for it to return BEFORE interval expires. Inside
> SDL_systimer.c select and nanosleep syscalls wrapped by do-while loop
> catching all EINTRs.
>
> I've tested this on two machines:
> Cel355 with 128M running RedHat 7.3
> and
> AMD Duron 1200 with 512M running RedHat 9
>
> Could someone explain that?
>
> --
> fuxx
>
>
> _______________________________________________
> SDL mailing list
> SDL at libsdl.org
> http://www.libsdl.org/mailman/listinfo/sdl
--
+-----------------------------------+
+ Bob Pendleton: independent writer +
+ and programmer. +
+ email: Bob at Pendleton.com +
+-----------------------------------+
More information about the SDL
mailing list