Clojure provides three functions to alter refs, as follows:
- ref-set
- alter
- commute
Both ref-set and alter are used in situations in which two or more threads try to mute ref, and one of them succeeds. This causes the others to fail. In this situation, the other transactions will start again, with the newly updated value of ref. This mechanism ensures that transactions work with consistent states.
TheĀ commute function is used in situations in which it does not matter what the most recent value is (only that it is recent).
Before we look at the functions in action, you will need to learn about Clojure's implementation of Software Transactional Memory, used to manage all of the changes that are applied to refs.