Skip to content

Instantly share code, notes, and snippets.

@ckampfe
Created July 10, 2025 23:26
Show Gist options
  • Select an option

  • Save ckampfe/e1ec58869fd2456489018949a7f99f6e to your computer and use it in GitHub Desktop.

Select an option

Save ckampfe/e1ec58869fd2456489018949a7f99f6e to your computer and use it in GitHub Desktop.
#!/usr/bin/env elixir
Mix.install([{:cas, git: "https://github.com/ckampfe/cas"}])
defmodule M do
alias Cas.Atom
def main_cas do
atom = Atom.new(1)
collected =
1..100
|> Task.async_stream(fn _ ->
Process.sleep(1)
out = Atom.swap!(atom, fn value -> value + 1 end)
# IO.puts(out)
end)
|> Enum.map(fn {:ok, v} -> v end)
|> IO.inspect()
IO.puts(Enum.count(collected))
IO.puts(Enum.count(MapSet.new(collected)))
end
def main_nocas do
:ets.new(:foo, [:public, :set, :named_table])
:ets.insert(:foo, {:v, 1})
collected =
1..100
|> Task.async_stream(fn _ ->
[{:v, i}] = :ets.lookup(:foo, :v)
incremented = i + 1
Process.sleep(1)
:ets.insert(:foo, {:v, incremented})
incremented
end)
|> Enum.map(fn {:ok, v} -> v end)
|> IO.inspect()
IO.puts(Enum.count(collected))
IO.puts(Enum.count(MapSet.new(collected)))
end
end
M.main_cas()
M.main_nocas()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment