From 16b1dec308e65abbc9a307d14ca45b17223c0713 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Tue, 27 Oct 2015 19:15:49 +0100 Subject: Add counter --- counter.erl | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 counter.erl (limited to 'counter.erl') 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}. -- cgit v1.2.3