Hi Everyone.

I have been reading the "Joy of Clojure" with great interest, but I
may have noticed a possible problem with listing 11.5. For those of
you who don't have the book, here is the listing:

(defn make-safe-array [ t sz]
  (let [a (make-array t sz)]
    (reify
      SafeArray
      (count [ _] ( clj/count a))
      (seq [_] (clj /seq a))
      (aget [_ i]
            ( locking a
              (clj /aget a i)))
      (aset [ this i f]
            ( locking a
              (clj /aset a i (f ( aget this i))))))))

This listing is an attempt to make the function safe for concurrent
modification. My claim is that count and seq should also be locking
around "a" for exactly same reason as aget and aset. In particular,
locking is not only ensuring mutual exclusion but also *visibility*.
The danger is that count and seq may be accessing stale values of "a".
See Java Concurrency in Practice, Section 3.1 for more of a
discussion. My reasoning is based on the assumption that locking is
simply an abstraction for the Java synchronized keyword. That
assumption may or may not be correct.

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to