Reputation: 115
I'm still trying to come to grips with Erlang messages, and while playing around with it, I came up with this case. It seems like it should work, but it just hangs indefinitely.
Can someone who is more used to Erlang please explain what I am doing wrong? And yes, I am aware that I don't even look at what is returned. This is the result of trying to reduce the code to isolate the problem.
-module(test).
-export([caller/2]).
callee(V1, V2, From) ->
From ! {V1, V2}.
caller(V1, V2) ->
spawn(fun() ->
callee(V1, V2, self()) end),
receive
_ ->
{V1, V2}
end.
Upvotes: 2
Views: 43
Reputation: 26121
This bit will help you understand where is the problem.
1> Self = self(), spawn(fun() -> io:format("Self: ~p, self():~p ~n", [Self, self()]) end).
Self: <0.83.0>, self():<0.85.0>
<0.85.0>
Upvotes: 1
Reputation: 811
If you assign the result of self()
to a variable outside the function in the call to spawn
and then pass in that variable instead of the literal self()
then the message sent in callee
will correctly be sent to the process running caller
(the process waiting on receive
).
Upvotes: 1