[SDL] Why not nanosleep()?

Loren Osborn sondheim at mail.lokigames.com
Tue Apr 4 10:11:07 PDT 2000

> Nicholas Vining wrote:
> > 
> > Huh. What's the resolution of the internal Pentium timers? It's gotta be
> > better than SDL's current resolution, and if that's really important, we
> > could go that route and start doing things that way. I have improved
> > resolution timer code for both x86 Pentium machines and Mac PPCs, if anybody
> > wants.
> That would result in a 'busy wait'... The trick is you want the system
> to suspend your task for the time you sleep so we can't change code in
> SDL to achieve this :) One thing that could be done is that when the
> argument to sleep is less than 10ms (on i386) it could do a busy wait,
> but I don't think this would be a good idea...
> -- 
> Daniel Vogel                           My opinions may have changed,
> 666 @ http://grafzahl.de               but not the fact that I am right

Something that I have seen done, with varying amounts of sucess is something
like: (Please excuse my pseudo-code)

	starttime = gettimeofday()
	if ( x < 10ms )		/* Give other processes a change */
	} else {
	endtime = gettimeofday()
	while ( starttime + x > endtime ) { /* while time left */
		endtime = gettimeofday()

The problem is that sleep(0) isn't as predictable/portable as we might
hope... If I remember correctly from FileNET, something like this works
reasonably well on WinNT, but I wouldn't trust it on Linux.



More information about the SDL mailing list