Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

also: defs, java fields, agents

The clojure website has documentation for these concepts:

I understand the words, but I don't conceptually get the purpose / meaning of these. When should I / how can I know when to use them?

share|improve this question
up vote 10 down vote accepted
  • Use Refs for synchronous, coordinated and shared changes.
  • Use Agents for asynchronous, independent and shared changes.
  • Use Atoms for synchronous, independent and shared changes.
  • Use Vars for isolated changes.

The links you posted are highly important to read, however I also recommends this: Software Transactional Memory

Keep in mind that aborted transactions will be replayed. What does this mean?
It means that the code inside dosync could be executed a couple of times, so you need to be careful the code should be side effects free.

One use of agents is to have a controlled side effects. Consider this:

  (send log-agent message args)
  (alter reference function)) 

The STM will hold all of the actions needed to be sent to agents until succeed.

What is difference between atoms and agents?
Updates to agents will happen asynchronously at some point of the future, updates to atoms will happened immediately.

Atoms and agents don't need to run inside a transaction, refs should be (coordinated changes, remember?)

share|improve this answer
This is starting to make sense... When would I use an agent instead of an atom? And what does the coordinated part of Refs mean? – naiad Apr 7 '12 at 2:49
An agent is good for sending jobs to that you want to get done but don't care when they happen. You might not care exactly when a log gets written to, just as long as it does get written to eventually. – WuHoUnited Apr 7 '12 at 3:31

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.