The basis of Clojure's concurrency is its Software Transactional Memory (STM) system. Basically this extends the semantics of database transactions to the computer's memory.
The way the STM works is that we mark memory
locations to be controlled by the STM using the ref
function. We can then de-reference those anywhere using the deref
function or the @
macro. But we can only change the values of a reference inside a dosync
block. Then, when the point of execution gets to the end of a transaction, the STM performs a check. If any of the references that the transaction altered have been changed by another transaction, the transaction fails, and it's queued to be tried again. However, if none of the references have changed, then the transaction succeeds and is committed.
While we're in the transaction, to code outside it, those values don't appear to have changed. Once the transaction is committed, then any changes we
make to those locations with ref-set...