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

Joe Armstrong erlang@REDACTED
Mon Feb 22 12:51:46 CET 2016


On Mon, Feb 22, 2016 at 12:04 PM, Jesper Louis Andersen
<jesper.louis.andersen@REDACTED> wrote:
>
> 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
> errors.
>
> 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.

I get the same effect irrespective of the sleep interval if I sleep for 100 ms
then the wake-up still can be still 6ms later than the requested time ...

Is the timer code in linux and OS-X different, or do they call the
same libraries
and the libraries behave differently?

I have no idea

/Joe



>
> -module(t).
> -compile(export_all).
>
> test() ->
>     process_flag(priority, max),
>     test(10, []).
>
> test(0, L) ->
>     L;
> test(K, L) ->
>     T1 = erlang:monotonic_time(),
>     erlang:send_after(1000, self(), ping),
>     receive
>       ping ->
>         T2 = erlang:monotonic_time(),
>         test(K-1, [erlang:convert_time_unit(T2-T1, native, micro_seconds) |
> L ])
>     end.
>
>
>
>
> --
> J.



More information about the erlang-questions mailing list