Updating refs
Refs, unlike atoms are a coordinated. They are however like Atoms, synchronous. All updates to refs must be done within a transaction. This can be done by wrapping the changes within a dosync block.
Alter
alter is the most like swap!. It will always be applied in the order designed and it makes no assumptions the re-orderability.
Commute
Commute is used when the order the changes are applied does not matter. It gets its name from commutative. Commute never attempts to retry and as such can be significantly faster.
Ref-set
Ref-set, like alter will retry on values. It is generally only used to to reinitialise refs state to starting values.
Validators
Validators must return true for a transactional change to take place. They can be applied using :validator fn.
The dark side of STM
The problem with all these retries that the STM provides is that it has no idea if what you're doing is unsafe. If you're writing to database, you don't want it written multiple times while another part of your application fails and the whole thing retries. For that reason there is an io! macro which throws an error if done within a transaction.
No comments:
Post a Comment