Skip to content

Instantly share code, notes, and snippets.

@pmbauer
Created May 15, 2013 17:19
Show Gist options
  • Save pmbauer/5585639 to your computer and use it in GitHub Desktop.
Save pmbauer/5585639 to your computer and use it in GitHub Desktop.
;; based on my code from test.benchmark
(ns raw-threadring
(:import [java.util.concurrent Exchanger]))
(def ^Exchanger output-pipe (Exchanger.))
(defn relay [state msg]
(if (> msg 0)
(send (:next state) relay (dec msg))
(.exchange output-pipe (:id state)))
state)
(defn make-ring [n]
(let [tail (agent {:id n})
head (reduce (fn [next id] (agent {:id id :next next}))
tail
(range (dec n) 0 -1))]
(await (send tail assoc :next head))
head))
(defn run [agents iterations]
(let [head (make-ring agents)]
(send head relay iterations)
(.exchange output-pipe nil)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment