summaryrefslogtreecommitdiff
path: root/counter.erl
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2015-10-27 19:15:49 +0100
committerDimitri Sokolyuk <demon@dim13.org>2015-10-27 19:15:49 +0100
commit16b1dec308e65abbc9a307d14ca45b17223c0713 (patch)
tree7e81c2a3c03e5c7bfa67efaf4d655d425074fb53 /counter.erl
parentdfdff99cd81b8a1d644b871967f35f57474a89db (diff)
Add counter
Diffstat (limited to 'counter.erl')
-rw-r--r--counter.erl25
1 files changed, 25 insertions, 0 deletions
diff --git a/counter.erl b/counter.erl
new file mode 100644
index 0000000..5146115
--- /dev/null
+++ b/counter.erl
@@ -0,0 +1,25 @@
+-module(counter).
+
+-export([start/0, count/1, next/0, stop/0]).
+
+start() -> register(counterPid, spawn(?MODULE, count, [0])).
+
+count(N) when N > 32767 ->
+ count(0);
+count(N) ->
+ receive
+ {next, FromPID} ->
+ FromPID ! {next, N};
+ {stop} ->
+ exit(normal)
+ end,
+ count(N+1).
+
+next() ->
+ counterPid ! {next, self()},
+ receive
+ {next, N} -> N
+ end.
+
+stop() ->
+ counterPid ! {stop}.