Skip to content

Instantly share code, notes, and snippets.

@killme2008
Created November 8, 2015 08:04
Show Gist options
  • Save killme2008/ac34c43b457cd43d8490 to your computer and use it in GitHub Desktop.
Save killme2008/ac34c43b457cd43d8490 to your computer and use it in GitHub Desktop.
Elixir process chain test.
defmodule Chain do
def counter(pid) do
receive do
n ->
send pid, n+1
end
end
def create_processes(n) do
last = Enum.reduce 1..n, self(), fn (_, pid) ->
spawn(Chain, :counter, [pid])
end
send last, 0
receive do
final_ansower when is_integer(final_ansower) ->
"Result is #{inspect(final_ansower)}."
end
end
def run(n) do
IO.puts inspect(:timer.tc(Chain, :create_processes, [n]))
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment