[erlang-questions] Sending message at a specific and accurate time

Jesper Louis Andersen jesper.louis.andersen@REDACTED
Mon Feb 22 12:04:17 CET 2016

On Mon, Feb 22, 2016 at 8:30 AM, Joe Armstrong <erlang@REDACTED> wrote:

> Here's the slightly modified program.

I *have* to nitpick this since I worry people will misuse the new 18.0
timing API. Changes to the program:

* Use erlang:monotonic_time() which gives you native resolution by default
according to the precision of your systems clock (older devices will give
you micro_seconds, newer systems nano_seconds). Also, by using
monotonic_time we avoid time warping, which you may get with system_time.

* Request the timestamps in native format, operate on the native format and
finally convert down to the desired output format. This avoids rounding

I get the same bad results on OSX, but not on Linux. It would be
interesting to analyze why it takes so long to wake up on OSX after having
slept for 1 second. It definitely looks like a problem with being woken up
and having to do work different from the scheduler loop, but I have yet to
investigate why that happens.


test() ->
    process_flag(priority, max),
    test(10, []).

test(0, L) ->
test(K, L) ->
    T1 = erlang:monotonic_time(),
    erlang:send_after(1000, self(), ping),
      ping ->
        T2 = erlang:monotonic_time(),
        test(K-1, [erlang:convert_time_unit(T2-T1, native, micro_seconds) |
L ])

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20160222/43049dc1/attachment.htm>

More information about the erlang-questions mailing list