-
-
Save fabjan/2597916 to your computer and use it in GitHub Desktop.
Erlang equivalent of http://return1.net/blog/2012/May/3/introduction-to-threads-with-c11
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(foo). | |
-compile(export_all). | |
%%% Prints the given string `Num_times` times, with `Time_between` milliseconds | |
%%% between each print. | |
printer(Text, Num_times, Time_between) -> | |
[begin | |
io:format(Text), | |
% Wait a moment before next print | |
timer:sleep(Time_between) | |
end | |
|| _ <- lists:seq(1, Num_times)]. | |
main() -> | |
Printers = | |
[spawn_link(?MODULE, printer, ["Hello~n", 30, 10]), | |
spawn_link(?MODULE, printer, ["World~n", 40, 15]), | |
spawn_link(?MODULE, printer, ["Parallel World~n", 40, 20])], | |
process_flag(trap_exit, true), | |
lists:foreach(Printers, fun (Printer) -> | |
receive | |
{'EXIT', Printer, normal} -> ok | |
end | |
end). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment