References

This page summarizes some of Clojure's key concepts regarding references (etc). It also provides links to more detailed information.

Clojure provides several "safe" ways to change data in the face of concurrent access (eg, by multiple threads).

Agents

Clojure's Agents allow independent, asynchronous changes to individual locations. The caller places an action (a "pure" function with optional arguments) on the location's queue. The caller may then proceed or wait, as appropriate. At some point in the future, the location's agent will (attempt to) perform the action.

Atoms

Clojure's Atoms provide a way to manage shared, synchronous, independent state. Like an Agent, an Atom is changed by the application of a "pure" function. However, any change will occur immediately (if ever).

Refs and STM

Clojure's Refs allow safe shared use of mutable storage locations, by means of a built-in Software Transactional Memory (STM) system. As discussed above, data access in an STM transaction is guaranteed to be atomic, consistent, and isolated (but not durable).

Each Ref points to a particular value, as of an instant in time. A Ref to the same object, taken later in time, may have a different value. However, each Ref's target object is guaranteed to remain unchanged. Access is performed in an optimistic, speculative manner; if a conflict occurs, the "losing" access simply retries.

Vars

Clojure's Vars provide a mechanism to bind (and dynamically rebind) symbols (etc) to different values. Because the binding is thread-local and obeys a stack discipline, Vars are safe to use in concurrent programs.


This wiki page is maintained by Rich Morin, an independent consultant specializing in software design, development, and documentation. Please feel free to email comments, inquiries, suggestions, etc!

Topic revision: r2 - 02 Mar 2013, RichMorin
This site is powered by Foswiki Copyright © by the contributing authors. All material on this wiki is the property of the contributing authors.
Foswiki version v2.1.6, Release Foswiki-2.1.6, Plugin API version 2.4
Ideas, requests, problems regarding CFCL Wiki? Send us email