Created
April 5, 2018 00:16
-
-
Save whilo/a394f5e63ff8dbce54f1816531cb45a1 to your computer and use it in GitHub Desktop.
Clojure STM example
This file contains 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
(def accounts | |
(into {} (for [i (range 1000001)] | |
[i (ref 1000)]))) | |
(time | |
(let [fs (dotimes [offset 10] | |
(future | |
(doseq [i (shuffle (range (* offset 100000) | |
(* (inc offset) 100000)))] | |
(dosync | |
(alter (accounts i) + 100) | |
(alter (accounts (inc i)) - 100)))))] | |
(doseq [f fs] | |
@f))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Lazily create accounts like this: