Skip to content
Fast semaphore using ETS.
Branch: master
Clone or download
daisyzhou Increment minor version to 1.1.0 (#4)
New features have been added such as call_linksafe
Latest commit 95b1363 Mar 14, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
lib Simplify matches and ets usage (#1) Sep 12, 2018
test [semaphore] implement a `Semaphore.call_linksafe/3`. Sep 12, 2018
.gitignore Publish to Hex Feb 20, 2017
.travis.yml Publish to Hex Feb 20, 2017
LICENSE Initial commit Feb 15, 2017
README.md Increment minor version to 1.1.0 (#4) Mar 14, 2019
mix.exs Increment minor version to 1.1.0 (#4) Mar 14, 2019

README.md

Semaphore

Master Hex.pm Version

Programming in Erlang and Elixir usually allows for no locking since the VM essentially handles it for you when communicating between processes. However, what about the situation when you have thousands of processes attempting to interact with a single resource such as a process? Usually they will overload the process and explode the message queue. ETS is the Swiss Army knife of the Erlang VM and can be applied to this problem. By using :ets.update_counter and :write_concurrency we can achieve a fast low contention semaphore on ETS.

Usage

Add it to mix.exs

defp deps do
  [{:semaphore, "~> 1.1"}]
end

Then just use it like a semaphore in any other language.

if Semaphore.acquire(:test, 1) do
  IO.puts "acquired"
  Semaphore.release(:test)
end

case Semaphore.call(:test, 1, fn -> :ok end) do
  :ok ->
    IO.puts "success"
  {:error, :max} ->
    IO.puts "too many callers"
end

License

Semaphore is released under the MIT License. Check LICENSE file for more information.

You can’t perform that action at this time.