Slide 9
Slide 9 text
spawn: goto FOR CONCURRENCY
(Don’t worry too much about the syntax. Erlang is weird.)
%% This is a recursive function with two clauses.
%% It counts backwards because that's less code.
say(_, 0) ->
ok;
say(Msg, N) ->
io:fwrite("~w ~s~n", [N, Msg]),
timer:sleep(1000),
say(Msg, N-1).
main(_) ->
spawn(fun() -> say("task1", 5) end),
spawn(fun() -> say("task2", 5) end),
timer:sleep(6000).
1
2
3
4
5
6
7
8
9
10
11
12
13
say(_, 0) ->
ok;
say(Msg, N) ->
io:fwrite("~w ~s~n", [N, Msg]),
timer:sleep(1000),
say(Msg, N-1).
%% This is a recursive function with two clauses.
1
%% It counts backwards because that's less code.
2
3
4
5
6
7
8
9
main(_) ->
10
spawn(fun() -> say("task1", 5) end),
11
spawn(fun() -> say("task2", 5) end),
12
timer:sleep(6000).
13
main(_) ->
spawn(fun() -> say("task1", 5) end),
spawn(fun() -> say("task2", 5) end),
timer:sleep(6000).
%% This is a recursive function with two clauses.
1
%% It counts backwards because that's less code.
2
say(_, 0) ->
3
ok;
4
say(Msg, N) ->
5
io:fwrite("~w ~s~n", [N, Msg]),
6
timer:sleep(1000),
7
say(Msg, N-1).
8
9
10
11
12
13
%% This is a recursive function with two clauses.
%% It counts backwards because that's less code.
say(_, 0) ->
ok;
say(Msg, N) ->
io:fwrite("~w ~s~n", [N, Msg]),
timer:sleep(1000),
say(Msg, N-1).
main(_) ->
spawn(fun() -> say("task1", 5) end),
spawn(fun() -> say("task2", 5) end),
timer:sleep(6000).
1
2
3
4
5
6
7
8
9
10
11
12
13
9