Created
April 18, 2013 20:38
-
-
Save selbyk/5416036 to your computer and use it in GitHub Desktop.
10 threads manipulating one shared data structure, which consists of 100 vectors each one containing 10 (initially sequential) unique numbers. Each thread then repeatedly selects two random positions in two random vectors and swaps them. All changes to the vectors occur in transactions by making use of Clojure's software transactional memory sys…
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
(defn run [nvecs nitems nthreads niters] | |
(let [vec-refs (vec (map (comp ref vec) | |
(partition nitems (range (* nvecs nitems))))) | |
swap #(let [v1 (rand-int nvecs) | |
v2 (rand-int nvecs) | |
i1 (rand-int nitems) | |
i2 (rand-int nitems)] | |
(dosync | |
(let [temp (nth @(vec-refs v1) i1)] | |
(alter (vec-refs v1) assoc i1 (nth @(vec-refs v2) i2)) | |
(alter (vec-refs v2) assoc i2 temp)))) | |
report #(do | |
(prn (map deref vec-refs)) | |
(println "Distinct:" | |
(count (distinct (apply concat (map deref vec-refs))))))] | |
(report) | |
(dorun (apply pcalls (repeat nthreads #(dotimes [_ niters] (swap))))) | |
(report))) | |
(run 100 10 10 100000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment