Created
November 21, 2025 05:26
-
-
Save BRonen/b394bd2151a7926aed588c309e70f97b to your computer and use it in GitHub Desktop.
Compare-and-Swap invalidation experiment using clojure atoms
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
| (ns atom-cas | |
| (:require [clojure.core.async :as async])) | |
| (def a (atom 0)) | |
| (defn updater [current-value] | |
| (prn "Thread: " (Thread/currentThread)) | |
| (prn "Current: " current-value) | |
| (Thread/sleep 500) | |
| (inc current-value)) | |
| (defn run-test [] | |
| (println "Start: " @a) | |
| (let [futures (doall (for [i (range 5)] | |
| (future | |
| (swap! a updater))))] | |
| (doseq [f futures] @f)) | |
| (prn "End: " @a) | |
| (prn "Expected: 5") | |
| (reset! a 0)) | |
| (comment | |
| (run-test) | |
| ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment