In Clojure we use STM for Concurrency.
My question is STM uses point in time value of data , isn't this introduce ambiguity ?
How could we know what value is accessed ?
The STM in Clojure provides (through ref
s and dosync
) a transaction context where all updates are guaranteed to be made "at the same time" to all the refs involved when viewed from the outside world.
The objective is to maintain consistency of the values in the system, the typical example is the transfer of money between two bank accounts. If you are transferring, say, 100 dollars from account A to account B, then you'll want to have the amounts for A and B changed simultaneously.
In this example there's actually no ambiguity in the values read for the amounts that are being handled inside the transaction, because only the following situations are possible at the moment the read from outside the transaction is done:
When inside the transaction, the ref
s you only read (and don't modify) could change their value from one point of the transaction to the other, this is called write skew (see Clojure Programming - Chapter 4, Refs, Write Skew). In order to avoid this you can use ensure
(instead of deref
), this will cause that if the value for any of these ref
s changes (the ones you only read), then the whole transaction will be retried.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With