> I understand that lazy-seq caches the computed result as each element is 
> read. However, how can is this made to be thread-safe?

The caching is done within a synchronized method as each element's value is 
realized.

There is an instance of clojure.lang.LazySeq that manages each seq element's 
value. The value is computed within a call to clojure.lang.LazySeq::sval which 
is synchronized. After the first time it's computed, the value is cached (in 
the sv member) and subsequent calls to sval return the cached value.

https://github.com/clojure/clojure/blob/master/src/jvm/clojure/lang/LazySeq.java#L37

http://docs.oracle.com/javase/tutorial/essential/concurrency/syncmeth.html

--Steve

-- 
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
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to