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

Reply via email to