Scatter / Gather (1 of 4)
- Use the message queue to employ all CPUs or cores to compute a solution
scatter(Fun, List) ->
S = self(),
Pids = [fun(I) ->
spawn(fun() -> exec_fun(S, Fun, I) end)
end || I <- List],
gather(Pids).
gather([]) -> [];
gather([H|T]) ->
receive
{H, Return} -> [Return|gather(T)]
end.
exec_fun(Parent, Fun, Item) ->
Parent ! {self(), catch(Fun(Item))}.