| 1 |
%% A process whose only job is to keep a counter. |
| 2 |
%% First version |
| 3 |
-module(counter). |
| 4 |
-export([start/0, codeswitch/1]). |
| 5 |
|
| 6 |
start() -> loop(0). |
| 7 |
|
| 8 |
loop(Sum) -> |
| 9 |
receive |
| 10 |
{increment, Count} -> |
| 11 |
loop(Sum+Count); |
| 12 |
{counter, Pid} -> |
| 13 |
Pid ! {counter, Sum}, |
| 14 |
loop(Sum); |
| 15 |
code_switch -> |
| 16 |
?MODULE:codeswitch(Sum) |
| 17 |
% Force the use of 'codeswitch/1' from the latest MODULE version |
| 18 |
end. |
| 19 |
|
| 20 |
codeswitch(Sum) -> loop(Sum). |
| 21 |
|
| 22 |
% Print the data using the pretty print specifier |
| 23 |
io:format("Here is the data: ~p~n", [Data]). |