Skip to content

Instantly share code, notes, and snippets.

@RumataEstor
Created February 6, 2014 05:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RumataEstor/8838955 to your computer and use it in GitHub Desktop.
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(), ...)`.
-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