It looks to me like the future sees the re-binding whereas the native thread does not! Like you, I'd expect the same behaviour from these 2 lines...Can anyone shine some light on this? I'm suspecting it has something to do with the thread pooling that is going on but I've got nothing to base this on...

Jim


On 03/08/12 08:49, Daniel Silén wrote:
Hi!

I am new to the language, and at the moment playing around with concurrent-constructs as presented in a series on Clojure concurrency at pluralsight.com (by Chraig Andera). When playing with thread-local redefinitions of vars via the binding-macro, I found an unexpected behavior: A future is used to create a background-thread, and I would expect it to behave the same way as when creating an explicit java thread, however it doesn't. If I rebind a var's value it receives the new value - but it shouldn't, because it is in another thread, right?!

Example:
(def ^:dynamic foo :fooval)
(def p (promise))
(def p2 (promise))
(binding [foo :newval]
  (future (deliver p foo))
  (.start (Thread. #(deliver p2 foo)))
(println "in future: " @p) ; expected value of @p :fooval, actual :newval
  (println "in java thread: " @p2)
  (println "in binding: " foo))
(println "outside binding: " foo)

produces (in clojure 1.4.0):
user=> in future:  :newval
in java thread:  :fooval
in binding:  :newval
outside binding:  :fooval
nil

I would expect the future-row to return the same as the java-thread-row, :fooval.

I would love to hear an expert-explanation... What did I do wrong? :)

Kind regards

Daniel Silén
--
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 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