Skip to content

Instantly share code, notes, and snippets.

@shoz-f
Created October 1, 2018 12:13
Show Gist options
  • Select an option

  • Save shoz-f/2e5097701ef6f23fc08284f45fcfb468 to your computer and use it in GitHub Desktop.

Select an option

Save shoz-f/2e5097701ef6f23fc08284f45fcfb468 to your computer and use it in GitHub Desktop.
プリミティブなプロセスで実装
defmodule StopwatchB do
import System, only: [ monotonic_time: 1 ]
@gclock __MODULE__
def activate() do
now = monotonic_time(100)
pid = spawn(StopwatchB, :watch_loop, [now])
Process.register(pid, @gclock)
:ok
end
def watch_loop(last_time) do
receive do
{:restart, caller} ->
now = monotonic_time(100)
send caller, {:ok, now - last_time}
watch_loop(now)
{:lap, caller} ->
now = monotonic_time(100)
send caller, {:ok, now - last_time}
watch_loop(last_time)
end
end
def restart() do
send @gclock, {:restart, self()}
receive do
{:ok, time} -> time
end
end
def lap() do
send @gclock, {:lap, self()}
receive do
{:ok, time} -> time
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment