Saturday, May 16, 2009

Clojure, parallel execution and closures

For completeness, here is code that should be pretty close to the threaded version of SBCL.  I decided to use the parallel map function instead of setting up explicit threads because of it's simplicity.  As expected, since we have to use a ref inside the closure which uses transactions to coordinate access, the result is always correct.

;; import random
(def random (java.util.Random.))

;; Closure containing state
(let [x (ref 0)]
  (defn incr1 [] (dosync (ref-set x (inc @x))))
  (defn show-incr1-val [] (print @x))
)

;; Threading function sleeping for random time
(defn my-thread [thread-name]
  (loop [i 0]
    (when (< i 10)
      (println (format "thread %s: %s" thread-name (incr1)))
      (. java.lang.Thread sleep (.nextInt random 3000))
      (recur (inc i))))
)

;; Use pmap to run the function 10 times in parallel
(doall (pmap my-thread (range 0 10)))


And here is the end of the run:

thread 8: 96
thread 7: 97
thread 8: 98
thread 7: 99
thread 7: 100
(nil nil nil nil nil nil nil nil nil nil)
user=>




0 comments: