September 2013
Intermediate to advanced
548 pages
12h 25m
English
Recall the emphasis we made on list-at-a-time operations and
in particular the function lists:map/2? map/2 is
defined like this:
| | map(_, []) -> []; |
| | map(F, [H|T]) -> [F(H)|map(F, T)]. |
A simple strategy for speeding up our sequential programs would
replace all calls to map with a new version of map that I’ll call pmap, which evaluates all its arguments
in parallel.
| lib_misc.erl | |
| | pmap(F, L) -> |
| | S = self(), |
| | %% make_ref() returns a unique reference |
| | %% we'll match on this later |
| | Ref = erlang:make_ref(), |
| | Pids = map(fun(I) -> |
| | spawn(fun() -> do_f(S, Ref, F, I) end) |
| | end, L), |
| | %% gather the results |
| | gather(Pids, Ref). |
| | |
| | do_f(Parent, Ref, F, I) -> |
| | Parent ! {self(), Ref, (catch F(I))}. |
Read now
Unlock full access