Reputation: 21
Can someone please explain this line by line?
Many many thanks.
run() ->
Pid = spawn(fun ping/0),
Pid ! self(),
receive
pong -> ok
end.
ping() ->
receive
From -> From ! pong
end.
Upvotes: 1
Views: 140
Reputation: 26121
Line by line:
run() ->
Declare the function run/0
Pid = spawn(fun ping/0),
Spawns new process with initialization function ping/0
and store its pid to variable Pid
Pid ! self(),
Sends message containing the pid of current process (result of self/0
call) to the process which pid is stored in variable Pid
receive
Waits for a message (or amount of time if there is after clause)
pong -> ok
If there is a received message pong, return value ok
end.
End of receive
clause and dot also means there is end of function run/0
ping() ->
Declares the function ping/0
receive
Waits for a message ...
From -> From ! pong
When receiving anything, store it in variable From
and then send message pong
to the process determined by value in From
(pid of process executing run/0
function in this case)
end.
End of receive and also ping/0
function
Upvotes: 6