Common Concurrency Patterns

  • Cast (send msg)
A ! B
  • Event (get msg)
receive A -> A end
  • Call (RPC)
A ! {self(), B},
receive 
  {A, Reply} -> 
    Reply
  end
  • Callback
receive
  {From, A} ->
    From ! F(A)
  end

Some trickery follows ;).

Here something cool - Parallel RPC:

par_rpc([A, B, C], M)

par_rpc(Ps, M) ->
  Self = self(),
  Tags = map(fun(I) ->
               Tag = make_ref(),
               spawn(fun() ->
                       Val = rpc(I, M),
                       Self ! {Tag, Val}
                     end),
               Tag
               end, Ps),
   yield(Tags).

yield([]) -> [];
yield([H|T]) ->
  Val1 = receive {H, Val} -> Val end,
  [Val1|yield(T)].