2010/1/28 Konrad Hinsen <[email protected]>:
> The Clojure solution for your problem is an agent, which makes the access
> thread-safe:
>
> user> (def data-source (agent (cycle [1 2 3])))
> #'user/data-source
> user> (defn get-some-data [] (let [v (first @data-source)] (send data-source
> rest) v))

Wouldn't this make it possible for two threads to obtain the same
value of (first @data-source), then send two rest messages to the
agent?

A ref would not have this problem, though:

(def data-source (ref the_sequence))
(defn get-some-data [] (dosync (let [v (first @data-source)] (alter
data-source rest) v)))

Sincerely,
Michal

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

Reply via email to