On Feb 15, 7:17 am, bOR_ <boris.sch...@gmail.com> wrote:
> Getting a bit confused again. It seems possible to make counters from
> agents, atoms and refs
...
> Is there a time and place for all three of them? I am leaning towards
> always using the agent, as it can be send from transactions when the
> transaction finishes.

There's a place for all three, but not necessarily for counters.
Here's a good discussion:
http://groups.google.com/group/clojure/msg/fd0371eb7238e933

And here's an example of encapsulating a counter in a function:
http://groups.google.com/group/clojure/msg/1ee509f9cbcfa3f2

You probably don't want to use an agent for a counter, since multiple
threads might read (deref) the same value before it gets updated.

If you're using the counter within a transaction (dosync), your
counter must be a ref.  You should not modify (swap!) an atom inside a
transaction, because the transaction may be retried.  Modifying an
atom is a side effect, and side effects should not happen in
transactions.

If you're not using refs/transactions, then your counter can be an
atom.

-Stuart Sierra
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To post to this group, send email to clojure@googlegroups.com
To unsubscribe from this group, send email to 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to