June 2014
Intermediate to advanced
300 pages
7h 12m
English
Yesterday we looked at atoms. Today we’ll look at the other types of mutable variables provided by Clojure: agents and refs. Like atoms, agents and refs are both concurrency aware and work in concert with persistent data structures to maintain the separation of identity and state. When talking about refs, we’ll see how Clojure supports software transactional memory, allowing variables to be modified concurrently without locks and yet still retaining consistency.
An agent is similar to an atom in that it encapsulates a reference to a single value, which can be retrieved with deref or @:
| | user=> (def my-agent (agent 0)) |
| | #'user/my-agent |
| | user=> @my-agent |
| | 0 |
The value of an agent is ...
Read now
Unlock full access