[erlang-questions] Timer implementation models

Chaitanya Chalasani cchalasani@REDACTED
Mon Oct 30 23:35:21 CET 2017


Hi,

I have hundred’s of processes each running its own timers.
I can think of the following models. Wanted to know the pros and cons of each model.

** Model 1 **
loop1(State) ->
  receive
  after 1000 ->
      do_someting()
  end.

** Model 2 **
loop2(State) ->
  erlang:send_after(1000, self(), timeout),
  receive
    timeout ->
      do_someting(),
      erlang:send_after(1000, self(), timeout)
  end.

** Model 3 **
loop3(State) ->
  timer:send_interval(1000, self(), timeout),
  loop3a(State).

loop3a(State) ->
  receive
    timeout ->
      do_someting()
  end.

The above three models using the gen_server -

** Model 1 **
init(_Args) ->
  {ok, State, 1000}.

handle_info(timeout, State) ->
  do_someting(),
  {noreply, State, 1000}

** Model 2 **
init(_Args) ->
  erlang:send_after(1000, self(), timeout),
  {ok, State}.

handle_info(timeout, State) ->
  do_someting(),
  erlang:send_after(1000, self(), timeout),
  {noreply, State}

** Model 3 **
init(_Args) ->
  timer:send_interval(1000, self(), timeout),
  {ok, State}.

handle_info(timeout, State) ->
  do_someting(),
  {noreply, State}

/Chaitanya


More information about the erlang-questions mailing list