[erlang-questions] Counters

Bengt Kleberg bengt.kleberg@REDACTED
Mon Apr 25 07:34:10 CEST 2016


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 <impartialmusic@REDACTED>, 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
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions




More information about the erlang-questions mailing list