Skip to content

Instantly share code, notes, and snippets.

Created June 6, 2009 04:18
Show Gist options
  • Save anonymous/124688 to your computer and use it in GitHub Desktop.
Save anonymous/124688 to your computer and use it in GitHub Desktop.
;; http://spreadsheets.google.com/ccc?key=rQLD6jgTTV5OqXwHdXtrTyg
(ns clj.benchmark.threadring (:gen-class))
(defn relay [state hops]
(if (> hops 0)
(do (send-off (:next state) relay (dec hops)) state)
(do (prn (:id state)) (shutdown-agents) state)))
(defn agent-ring [n]
(let [tl (agent {:next nil :id n})
hd (reduce (fn [next id] (agent {:next next :id id})) tl (reverse (range 1 n)))]
(send tl #(assoc % :next hd)) ;; hook up head and tail
(await tl)
hd))
(defn run [n-agents hops]
(let [hd (agent-ring n-agents)]
(send-off hd relay hops)
:done))
(defn -main [arg]
(run 503 (Integer/parseInt arg)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment