Skip to content

Instantly share code, notes, and snippets.

@mattyw
Created June 17, 2013 21:13
Show Gist options
  • Select an option

  • Save mattyw/5800508 to your computer and use it in GitHub Desktop.

Select an option

Save mattyw/5800508 to your computer and use it in GitHub Desktop.

Revisions

  1. mattyw created this gist Jun 17, 2013.
    38 changes: 38 additions & 0 deletions spawn.exs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    import :timer, only: [sleep: 1]
    defmodule Main do
    @num 1000000

    def thread(pid) do
    :timer.sleep(10000)
    pid <- {:result, 1}
    end

    def collector(x) when x == @num do
    :done
    end

    def collector(x) do
    receive do
    {:result, n} ->
    IO.puts inspect n + x
    collector((x + n))
    end
    end

    def spawner(x) when x == @num do
    :done
    end

    def spawner(x) do
    spawn(__MODULE__, :thread, [self])
    spawner(x + 1)
    end

    def main() do
    spawner(0)
    collector(0)
    end

    end

    Main.main()