Accurate clock in Erlang
- by buddhabrot
I was thinking about how to implement a process that gives the number of discrete intervals in time that occurred since it started.
Am I losing accuracy here? How do I implement this without loss of accuracy after a while and after heavy client abuse. I am kind of stumped how to do this in Erlang.
-module(clock).
-compile([export_all]).
start(Time) ->
register(clock, spawn(fun() -> tick(Time, 0) end)).
stop() -> clock ! stop.
tick(Time, Count) ->
receive
nticks ->
io:format("~p ticks have passed since start~n", [Count])
after 0 -> true
end,
receive
stop ->
void
after Time ->
tick(Time, Count + 1)
end.