Created
May 18, 2018 04:46
-
-
Save micahboyd/29ebd2bad0f167d1904b890edc9d4979 to your computer and use it in GitHub Desktop.
Elixir Ping Pong
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| defmodule PingPong do | |
| def start do | |
| ping_process = spawn(Ping, :loop, []) | |
| pong_process = spawn(Pong, :loop, []) | |
| send(ping_process, {:pong, pong_process}) | |
| end | |
| def ping do | |
| Ping | |
| end | |
| end | |
| defmodule Ping do | |
| def loop(n \\ []) do | |
| receive do | |
| {:pong, sender_pid} -> | |
| :timer.sleep(1_000) | |
| send(sender_pid, {:ping, self()}) | |
| IO.puts("ping ----") | |
| loop() | |
| _ -> | |
| loop() | |
| end | |
| end | |
| end | |
| defmodule Pong do | |
| def loop(n \\ []) do | |
| receive do | |
| {:ping, sender_pid} -> | |
| :timer.sleep(1_000) | |
| send(sender_pid, {:pong, self()}) | |
| IO.puts("---- pong") | |
| loop() | |
| _ -> | |
| loop() | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment