I'm playing with processes in Erlang and trying to create a simple counter process. It receives a PID of a sender, increments inner counter and sends new counter value to the sender. I'm launching my code from erl shell (Erlang/OTP 20, Eshell V9.2). And I'm able to successfully receive first reply from the counter process, but not the second.
%% Process function that serves as a counter
CounterFun = fun() ->
(fun Receiver(Current) ->
io:format(" -- Entering receiver, current ~p~n", [Current]),
receive
Sender ->
New = Current + 1,
io:format(" -- Sending ~p to ~p~n", [New, Sender]),
Sender ! New,
Receiver(New)
end
end)(0)
end.
CounterPid = spawn(CounterFun).
CounterPid ! self().
receive V -> V after 3000 -> timeout end. % Will provide 1
CounterPid ! self().
receive V -> V after 3000 -> timeout end. % Will result in timeout
