summaryrefslogtreecommitdiff
path: root/counter.erl
blob: 915f5a9bb68af878005f8c454e8ba847b463f7c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
-module(counter).

-export([start/0, count/1, next/0, set/1, stop/0]).

start() ->
	register(counterPid, spawn(?MODULE, count, [0])).

count(N) when N > 32767 ->
	count(0);
count(N) ->
	receive
		{next, FromPID} -> 
			FromPID ! {next, N},
			count(N+1);
		{set, New} ->
			count(New);
		{stop} ->
			exit(normal)
	end.

set(N) ->
	counterPid ! {set, N}.

next() ->
	counterPid ! {next, self()},
	receive
		{next, N} -> N
	end.

stop() ->
	counterPid ! {stop}.