Mercurial > repos > other > SevenLanguagesInSevenWeeks
comparison 5-Erlang/infinirun.erl @ 77:7bab4843aec6
Add roulette with auto-restarting process
(And black-magic auto-registering of atoms)
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sat, 03 Feb 2018 20:42:51 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
76:5860845f5dac | 77:7bab4843aec6 |
---|---|
1 -module(infinirun). | |
2 -export([loop/0]). | |
3 | |
4 % To run, call: Restarter = spawn(fun infinirun:loop/0). | |
5 % Then call Restarter ! new to initiate | |
6 % This does black magic and registers a public atom called "revolver", which is a process running roulette:loop/0 that can receive messages | |
7 loop() -> | |
8 % Indicate that we want exist signals, which are a 3-tuple of { 'EXIT', From, Reason } | |
9 process_flag(trap_exit, true), | |
10 receive | |
11 new -> | |
12 % Start a new process | |
13 io:format("Creating and monitoring process.~n"), | |
14 register(revolver, spawn_link(fun roulette:loop/0)), | |
15 loop(); | |
16 {'EXIT', From, Reason} -> | |
17 io:format("The shooter ~p died with reason ~p. Restarting…~n", [From, Reason]), | |
18 % Send ourselves a message to register a new process | |
19 self() ! new, | |
20 loop() | |
21 end. |