ANN: crone - cron in Erlang (sort of)

Ulf Wiger etxuwig@REDACTED
Fri Jul 5 00:34:12 CEST 2002


On Thu, 4 Jul 2002, Chris Pressey wrote:

>It raises the question: just how large can the argument to
>timer:sleep/1 be?  Scheduling a task on a monthly basis means
>sleeping for potentially hundreds of hours (that is, billions of
>milliseconds.)  How much inaccuracy should be expected to creep
>in over such a long wait?

The argument to timer:sleep/1 can be a 32 bit integer, I think.
It's the same as T in receive after T -> ok end.

A function that allows for longer sleeps than that can be found
below. If you're worried about the precision (I don't think it's
a big problem, but have not tried to figure out exactly what it
is), you can reduce ?MAX_MSG_WAIT_TIME significantly without any
measureable performance penalty.

/Uffe

-define(MAX_MSG_WAIT_TIME, 4294967295).         % Max int. for
                                                % unsigned 32 bit

sleep(Time) ->
    do_sleep(Time, system_time()).

do_sleep(Time, Start) ->
    DiffTime = system_time() - Start,
    case Time - DiffTime of
        X when X > ?MAX_MSG_WAIT_TIME ->
            sleep(?MAX_MSG_WAIT_TIME),
            do_sleep(Time, Start);
        X when X > 0 ->
            receive
                after X ->
                        ok
                end;
        X ->
            ok
    end.


%%%
%%% return the system time in
%%% milli-seconds.
%%%

system_time() ->
    {M,S,U} = erlang:now(),
    1000000000 * M + 1000 * S + (U div 1000).


-- 
Ulf Wiger, Senior Specialist,
   / / /   Architecture & Design of Carrier-Class Software
  / / /    Strategic Product & System Management
 / / /     Ericsson Telecom AB, ATM Multiservice Networks




More information about the erlang-questions mailing list