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

Per Hedeland per@REDACTED
Thu Feb 19 21:06:16 CET 2009


James Hague <james.hague@REDACTED> wrote:
>
>> More than that, the runtime enforces that now/0 returns monotonically
>> increasing values, which makes it useful for timestamping events and
>> ensuring they have a stable sort order.
>
>Very useful...but not for now/0.  A function that returns the current
>time shouldn't itself cause the time to change.  now() is make_ref
>disguised as a clock.

I don't know if you actually meant it, but of course calling now/0 has
no effect on the VM's idea of time itself, only on subsequent return
values from now/0. Once you stop calling it once per microsecond or
more, the effect will go away.

In the day when this nifty feature was implemented, there existed no
HW/OS combination that could complete the underlying call to
gettimeofday() in less than a microsecond. However implementations that
kept returning the same number of microseconds until a timer interrupt
bumped the value by 10000 or more were common.

Maybe the time (sorry) has come to introduce really_now/0, which would
return the unadjusted result of calling clock_gettime(CLOCK_REALTIME) as
a single bignum (nanoseconds since the epoch). Using this number as the
basis for timeout calculations would be strictly forbidden.

--Per Hedeland



More information about the erlang-questions mailing list