https://clojuredocs.org/clojure.core/ensure

Must be called in a transaction. Protects the ref from modification
by other transactions.  Returns the in-transaction-value of
ref. Allows for more concurrency than (ref-set ref @ref)



This can be read in two contradictory ways. Protecting a ref during a 
transaction sounds like it increases contention and slows the app down. 
"Allows for more concurrency" can be read as "Allows you to use a huge 
number of refs while still ensuring consistency." In other words, it sounds 
like it offers safety at the cost of speed. 




On Monday, March 6, 2017 at 6:06:46 AM UTC-5, bertschi wrote:
>
> For a lecture I have implemented the write-skew example from Mark 
> Volkmann's article:
>
> (defn write-skew []
>   (let [cats (ref 1)
>         dogs (ref 1)
>
>         john (future
>                (dosync
>                  (when (< (+ @cats @dogs) 3)
>                    (alter cats inc))))
>         mary (future
>                (dosync
>                  (when (< (+ @cats @dogs) 3)
>                    (alter dogs inc))))]
>     (do @john @mary)
>     (dosync
>      (> (+ @cats @dogs) 3))))
>
> (defn write-skew-demo []
>   (let [count-write-skews (atom 0)]
>     (doseq [_ (range 25)]
>       (when (write-skew)
>         (swap! count-write-skews inc)))
>     @count-write-skews))
>
> (write-skew-demo)
>
> When I try to fix this program using ensure, i.e. using (ensure dogs) in 
> john and (ensure cats) in mary, I find that it sometimes runs very slow.
>
> From the docs it says "Allows for more concurrency than (ref-set ref 
> @ref)". I would read that as "runs at least as fast as (ref-set ref @ref)", 
> but using (ref-set dogs @dogs) instead of (ensure dogs) and the same with 
> cats, does not exhibit these occasional runtime spikes.
>
> Am I misunderstanding something or is it a bug in ensure?
>
>
> Nils
>

-- 
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/d/optout.

Reply via email to