[erlang-questions] writing a delay loop without now()

Dominic Williams erlang@REDACTED
Fri Feb 20 09:35:04 CET 2009


Hi James,

> I have a graphical application that runs at a fixed frame
> rate (60 frames per second).  The processing takes up part
> of the frame, then I need to make sure that roughly 16,667
> microseconds have passed before displaying the next frame.
> os:sleep() is VERY coarse and inconsistent across
> platforms.  On some platforms os:sleep(1) is the same as
> sleep:(32000).  Using a timeout value with receive gives
> the same results (that's actually how os:sleep is
> implemented).  erlang:statistics(wall_clock) is just as
> coarse.

I may not have had quite such stringent realtime
requirements as you, but I've achieved this sort of thing
by:

- calculating time left to the start of next frame

- using timer:send_after/2 to send myself a special message
  at start of next frame

- going into a blocking receive of the special message

Two variants of this are:

- using a separate process that produces the "start of
  frame" messages, using the same principle

- using timer:send_interval/2, which amounts to using OTP's
  built-in separate process that produces regular messages.

Have you tried such approaches?

Best regards,

Dominic Williams
http://dominicwilliams.net




More information about the erlang-questions mailing list