Created
February 6, 2014 05:49
-
-
Save RumataEstor/8838955 to your computer and use it in GitHub Desktop.
Shows how much different is the direct message send and `erlang:send_after(0, self(), ...)`.
This file contains hidden or 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(send_after_test). | |
-export([main/0]). | |
main() -> | |
Self = self(), | |
Ref = make_ref(), | |
spawn(fun() -> test(Self, Ref) end), | |
receive Ref -> ok end. | |
test(Master, Ref) -> | |
Self = self(), | |
Self ! 0, | |
erlang:send_after(0, Self, send_after), | |
send(Self, 1, 1000), | |
recv(0), | |
Master ! Ref. | |
send(Self, I, Max) -> | |
Self ! I, | |
if I >= Max -> | |
ok; | |
true -> | |
send(Self, I + 1, Max) | |
end. | |
recv(N) -> | |
receive | |
send_after -> | |
io:format("Received after ~p messages\n", [N]); | |
N -> | |
recv(N + 1); | |
Msg -> | |
io:format("Unexpected message ~p\n", [Msg]), | |
recv(N + 1) | |
end. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment