Created
July 10, 2025 23:26
-
-
Save ckampfe/e1ec58869fd2456489018949a7f99f6e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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