micro_timer v0.1.0 MicroTimer View Source

A timer module with microsecond resolution.

Link to this section Summary

Functions

Invokes the given executable after timeout microseconds with the list of arguments args

Invokes the given executable repeatedly every timeout microseconds with the list of arguments args

Send message to pid after timeout microseconds

Send message to pid every timeout microseconds

Suspend the current process for the given timeout and then returns :ok

Link to this section Types

Link to this type executable() View Source
executable() :: {module(), atom()} | function()

Link to this section Functions

Link to this function apply_after(timeout, executable, args \\ []) View Source
apply_after(non_neg_integer(), executable(), [any()]) :: pid()

Invokes the given executable after timeout microseconds with the list of arguments args.

executable can either be the tuple {Module, :function}, an anonymous function or a function capture.

Returns the pid of the timer.

See also cancel_timer/1.

Examples

MicroTimer.apply_after(250, {Module. :function}, [])

MicroTimer.apply_after(250, fn a -> a + 1 end, [1])

iex> pid = MicroTimer.apply_after(250, fn arg -> arg end, [1])
iex> is_pid(pid)
true
Link to this function apply_every(timeout, executable, args \\ []) View Source
apply_every(non_neg_integer(), executable(), [any()]) :: pid()

Invokes the given executable repeatedly every timeout microseconds with the list of arguments args.

executable can either be the tuple {Module, :function}, an anonymous function or a function capture.

Returns the pid of the timer.

See also cancel_timer/1.

Examples

MicroTimer.apply_every(250, {Module. :function}, [])

MicroTimer.apply_every(250, fn a -> a + 1 end, [1])

iex> pid = MicroTimer.apply_every(250, fn arg -> arg end, [1])
iex> is_pid(pid)
true
Link to this function cancel_timer(pid) View Source
cancel_timer(pid()) :: true

Cancel a timer pid created by apply_after/2, apply_after/3, apply_every/2 or apply_every/3

Always returns true

Examples

timer = MicroTimer.apply_every(250, {Module. :function}, [])
MicroTimer.cancel_timer(timer)

iex> pid = MicroTimer.apply_every(250, fn arg -> arg end, [1])
iex> MicroTimer.cancel_timer(pid)
iex> Process.alive?(pid)
false
Link to this function send_after(timeout, message, pid \\ self()) View Source
send_after(non_neg_integer(), any(), pid()) :: pid()

Send message to pid after timeout microseconds.

If pid is left empty, the message is sent to self().

Returns the pid of the timer.

See also cancel_timer/1.

Examples

MicroTimer.send_after(250, :msg)

MicroTimer.send_after(250, "msg", self())

iex> pid = MicroTimer.send_after(250, :msg)
iex> is_pid(pid)
true
Link to this function send_every(timeout, message, pid \\ self()) View Source
send_every(non_neg_integer(), any(), pid()) :: pid()

Send message to pid every timeout microseconds.

If pid is left empty, the message is sent to self().

Returns the pid of the timer.

See also cancel_timer/1.

Examples

MicroTimer.send_every(250, :msg)

MicroTimer.send_every(250, "msg", self())

iex> pid = MicroTimer.send_every(250, :msg)
iex> is_pid(pid)
true
Link to this function µsleep(timeout) View Source
µsleep(non_neg_integer()) :: :ok

Suspend the current process for the given timeout and then returns :ok.

timeout is the number of microsends to sleep as an integer.

Examples

iex> MicroTimer.usleep(250)
:ok