[SDL] Re: Why not nanosleep()?

Maxim Sobolev sobomax at altavista.net
Mon Apr 3 07:30:46 PDT 2000


Daniel Vogel wrote:

> Christian Bauer wrote:
> >
> > Hi!
> >
> > On Mon, 3 Apr 2000, Maxim Sobolev wrote:
> > > I'm wondering why the SDL library doesn't use POSIX conformant nanosleep()
> > > function in its SDL_Delay() routine. Currently select() used for this purpose,
> >
> > I also wondered a while ago and tried to replace the select() with
> > nanosleep() and usleep(). The result: under Linux 2.2.x, the select()
> > timeout seems to have a far higher accuracy and resolution than *sleep(),
> > e.g. nanosleep(3ms) on my machine usually waits for about 20-30ms,
> > sometimes 14-16ms, while select() is relatively constant at ~3ms.
>
> I did a 1000 x 1 ms sleep with 'sleep' == usleep it took 20 secs and
> with select only 10. Then I did try a 100x 10 ms sleep and it took 2
> seconds with select. Looks as if select takes about 10ms more than I
> want :( Well, at least it performs better than usleep.

Hm,

FreeBSD seems much more consistent in this area. My small testcase gave me (in ms of
course):

Requiested: 1, Nanosleep: 20, Select: 20
Requiested: 2, Nanosleep: 20, Select: 20
Requiested: 4, Nanosleep: 20, Select: 20
Requiested: 8, Nanosleep: 20, Select: 20
Requiested: 16, Nanosleep: 30, Select: 30
Requiested: 32, Nanosleep: 50, Select: 50
Requiested: 64, Nanosleep: 80, Select: 80
Requiested: 128, Nanosleep: 140, Select: 140
Requiested: 256, Nanosleep: 270, Select: 270
Requiested: 512, Nanosleep: 529, Select: 530

Well, while it is not very accurate, at least it is the same across different
interfaces.

-Maxim





More information about the SDL mailing list