Gtimer (gtimer v0.1.0)

A small library which provides a global timer facility in Elixir (or Erlang).

example

Example:

iex> timer_ref = Gtimer.new_timer(timeout,timeout_action \ nil)

Starts a new timer running for timeout millseconds, returns a "timer reference".

iex> Gtimer.cancel_timer(timer_ref)

Cancels a running timer.

If a running timer is not cancelled when the timer expires the timeout_action function will be called with the process identifier of the process that created the timer as a single argument. If the timeout_action is not a function of arity 1, then the default action of terminating the process that created the timer, and displaying an informative message, will be taken.

Timer management is done in a separate process. Inside the process timers are stored in a priority queue, and using a map, which affords logarithmic worst-case time complexity for both function calls (in terms of the number of timers running).

example-1

Example:

iex> timer_ref = Gtimer.new_timer(1000,fn pid -> Process.exit(pid,:because) end)

This will start a new timer which when it expires kills the process which invoked the call to new_timer.

Link to this section Summary

Functions

Cancels a running timer.

Returns a specification to start this module under a supervisor.

Starts a new timer running for timeout millseconds, returns a "timer reference". If a running timer is not cancelled when the timer expires the timeout_action function will be called with the process identifier of the process that created the timer as a single argument. If the timeout_action is not a function of arity 1, then the default action of terminating the process that created the timer, and displaying an informative message, will be taken.

Link to this section Functions

Link to this function

cancel_timer(time_ref)

Cancels a running timer.

Link to this function

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

Link to this function

new_timer(timeout, timeout_action \\ nil)

Starts a new timer running for timeout millseconds, returns a "timer reference". If a running timer is not cancelled when the timer expires the timeout_action function will be called with the process identifier of the process that created the timer as a single argument. If the timeout_action is not a function of arity 1, then the default action of terminating the process that created the timer, and displaying an informative message, will be taken.