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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
-module(a).
-export([start/1, stop/1]).
-export([pong/1]).
-export([loop/3,exit_kalle/0]).
%% start(N) -> pid()
%% N = integer()
start(N) ->
Pong = b:start(),
spawn(?MODULE, loop, [self(), N, Pong]),
receive
done ->
{exit,kalle} = trycatch(fun ?MODULE:exit_kalle/0),
{throw,kalle} = trycatch(fun() -> throw(kalle) end),
done
end.
%% stop(Ping) -> stop
%% Ping = pid()
stop(Ping) ->
Ping ! stop.
%% pong(Ping) -> pong
%% Ping = pid()
pong(Ping) ->
Ping ! pong.
%%--Internal functions------------------------------------------------
loop(Starter, N, Pong) when N>0 ->
Pong ! {ping, self()},
receive
pong ->
loop(Starter, N-1, Pong);
stop ->
done
end;
loop(Starter, 0, Pong) ->
Pong ! stop,
Starter ! done.
trycatch(Fun) ->
try Fun()
catch
Throw ->
{throw,Throw};
exit:Reason ->
{exit,Reason}
after
cleanup
end.
exit_kalle() ->
exit(kalle).
|