[erlang-questions] Counters

Garrett Smith <>
Mon Apr 25 15:27:16 CEST 2016


That's an interesting exercise. I personally wouldn't run that code
outside of tinkering/experimentation, primarily because it's not
easily used within an OTP supervisory hierarchy. That comes pretty
close to a strict rule in Erlang production systems - and in this case
in particular you'd want to formally account for your incr processes.

It's also unorthodox to use atoms that way, particularly when they can
grow unbounded by design (see Begnt's comment about memory).

I'm also trying to recollect when I ever needed a general purpose
counter. Typically any incrementing values are part of some other
process/service state. In that case, it's trivial to maintain a
counter internally. There's also ets/dets update_counter, in the event
you're keeping track of a large number of counters, or other stats as
well and it's convenient to use term storage.

On Mon, Apr 25, 2016 at 12:34 AM, Bengt Kleberg
<> wrote:
> Greetings,
>
> Two things, that I think should be mention, about the code below:
> The comment
> They will be transliterated into "counter_1"
> is wrong. The code suffixes "counter_" with the integer 1, which is not "1",
> but SOH.
> Also, it is good to comment each usage of erlang:list_to_atom/1 with an
> acknowledgment that it has the potential to exhaust memory. Not when use in
> moderation and definitely not in this case, but as a reminder, lest the
> problem be forgotten.
>
>
> bengt
>
> On 04/25/2016 12:36 AM, Donald Steven wrote:
>>
>> Hi all,
>>
>> With the archived help provided by Alpar Juttner and Witold Baryluk, I
>> thought I would share (primarily for other newbees like me), one way to
>> implement counters.
>>
>> Best,
>>
>> Don
>>
>>
>> ===================================================================================
>>
>>
>> %%%--------------------------------------------------------------------------
>> %%% File       : counter.erl, version 1.00
>> %%% Author     : Donald Steven <>, adapted from code
>> %%%              suggested by Alpar Juttner and Witold Baryluk of the
>> Erlang
>> %%%              community
>> %%% Purpose    : To demonstrate multiple independent counters
>> %%% Created    : 24 Apr 2016
>> %%% Invocation : erlc counter.erl
>> %%%              erl -noshell -s counter main -s init stop
>>
>> %%%--------------------------------------------------------------------------
>>
>> -module(counter).
>> -export([main/0,inc/1,dec/1,current/1,start/1,loop/1]).
>>
>>
>> %%%--------------------------------------------------------------------------
>> %%% Fun     : inc(X), dec(X), current(X)
>> %%% Purpose : Increment, decrement the counter X, and provide its current
>> %%%           state
>> %%% Created : 24 Apr 2016
>>
>> %%%--------------------------------------------------------------------------
>>
>> inc(X) ->
>>     list_to_atom("counter_" ++ [X]) ! inc_counter,
>>     ok.
>>
>> dec(X) ->
>>     list_to_atom("counter_" ++ [X]) ! dec_counter,
>>     ok.
>>
>> current(X) ->
>>     list_to_atom("counter_" ++ [X]) ! {get_counter, self()},
>>     receive
>>         { counter_value, Cnt } ->
>>             Cnt
>>     end.
>>
>>
>> %%%--------------------------------------------------------------------------
>> %%% Fun     : start(X) and loop(Counter)
>> %%% Purpose : Start the counter X
>> %%% Created : 24 Apr 2016
>>
>> %%%--------------------------------------------------------------------------
>>
>> start(X) ->
>>
>>     Pid = spawn(counter,loop,[0]),
>>     Name = list_to_atom("counter_" ++ [X]),
>>     register(Name, Pid),
>>     ok.
>>
>> loop(Counter) ->
>>     receive
>>         inc_counter ->
>>             NewCounter = Counter+1;
>>         dec_counter ->
>>             NewCounter = Counter-1;
>>         {get_counter, Pid} ->
>>             Pid ! { counter_value, Counter },
>>             NewCounter = Counter
>>     end,
>>     loop(NewCounter).
>>
>>
>> %%%--------------------------------------------------------------------------
>> %%% Fun     : main()
>> %%% Purpose : Demonstrate usage
>> %%% Created : 24 Apr 2016
>>
>> %%%--------------------------------------------------------------------------
>>
>> main() ->
>>
>>     I = 1,          % IMPORTANT: these are counter numbers, not values
>>     J = 2,          % They will be transliterated into "counter_1" and
>>                     % "counter_2" internal to the various funs
>>
>>     start(I),       % start counters I and J
>>     start(J),       % all counters start at 0
>>
>>     inc(I), inc(I),                 % increment and decrement 'out of
>> order'
>>     inc(J), inc(J), inc(J), inc(J), % to verify counter independence
>>     inc(I),                         % results should be counter I = 2 and
>>     dec(J),                         % counter J = 3
>>     dec(I),
>>
>>     io:format("Current counter I: ~p~n", [current(I)]),
>>     io:format("Current counter J: ~p~n", [current(J)]).
>> _______________________________________________
>> erlang-questions mailing list
>> 
>> http://erlang.org/mailman/listinfo/erlang-questions
>
>
> _______________________________________________
> erlang-questions mailing list
> 
> http://erlang.org/mailman/listinfo/erlang-questions


More information about the erlang-questions mailing list