Hi,

the commute case can be understood:

user=> (send thread (fn [agt aref] (dosync (commute aref + 100) 
(Thread/sleep 8000) agt)) account)
#<Agent@1950e0a: "Thread">
user=> (time (dosync (ref-set account 2000)))
"Elapsed time: 0.369415 msecs"
2000
user=> @account
2000
;; wait a few seconds
user=> @account
2100

So, the transaction run from the agent just recalculates the values at the 
end of the transaction again, based upon the value the ref has then.

See LockingTransaction.java:290:

    vals.put(ref, f.fn.applyTo(RT.cons(vals.get(ref), f.args)));

So in this case the ref-set from the REPL thread comes first.

As for the other effect, that the REPL thread blocks until the eight seconds 
from the agent-transaction are finished, we can clearly see that the ref-set 
call is the last comitter, because the ref has the value 2000 afterwards:

user=> (dosync (ref-set account 1000))
1000
user=> (send thread (fn [agt aref] (dosync (alter aref + 100) (Thread/sleep 
8000) agt)) account)
#<Agent@1950e0a: "Thread">
user=> (time (dosync (ref-set account 2000)))
"Elapsed time: 5241.963286 msecs"
2000
;; again wait a few seconds for good measure
user=> @account
2000

I have to confess, that I don't understand that, yet.   I was first guessing 
it could be related to the history, but the history is still zero:

user=> (.getHistoryCount account)
0

Unless someone else answers this I will try to find some time to dive into 
it this evening (CEST). Adding a few atoms as counters here and there 
usually helps understanding, what is going on under the hooks.

Regards,
Stefan

PS: Starting a thread with a transaction using future seems easier to me, 
e.g.:

(future (dosync (alter account + 100) (Thread/sleep 8000)))

-- 
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