Hi,
On Mar 15, 6:47 am, Mark Engelberg <[email protected]> wrote:
> What's a minimal example that demonstrates when ensure is necessary?
Ok. Maybe a little grotesque and not 100% valid, but it should get
across the idea.
(dosync
(when (= @reactor-state :off)
(alter reactor-door open)))
Once every 100 years we will open the reactor door while the reactor
is turned on, because between the deref of the reactor state and the
open command for the door some other transaction might turn on the
reactor. (That is: the transaction will not retry if the alter
succeeded.)
(dosync
(when (= (ensure reactor-state) :off)
(alter reactor-door open)))
This is safe, because the transaction retries even if the alter
succeeded. Namely in the case that the value of reactor state changed.
In short: ensure is used when you require only read access to a Ref,
but need the value to be constant through the transaction. If you
modify the a Ref via alter or ref-set, the ensure is not necessary.
Sincerely
Meikel
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en