Ken Wesson <kwess...@gmail.com> writes:

> I put a println in a swap! function once and the world didn't end.

For a println retries are usually harmless, it just means you'll
occasionally get doubled output.  But it's not a good thing for a
general-purpose runonce fn whose entire reason for existence is to
ensure something is called only once. ;-)

> And swap!s on a single atom are supposed to be atomic, any given swap!
> definitely happening before or after any other given swap! on the same
> atom.

The guarantee of atomicity is *dependent* on the function not having
side-effects.  If it has side-effects then there is no guarantee.   The
side-effects thing is not just about cleaner code or something, it is
entirely *necessary* for correct operation.

Atoms do not lock.  If multiple threads enter swap! simultaneously, they
*will* each execute f.  One will win and the others will retry.

Yes, this happening is rare unless there's a lot of  contention on the
atom, but that just makes the resulting heisenbugs much harder track
down.

Clojure makes concurrency safer, but it's not magic, it only works if
you play by its rules.  The rules are there for good reasons, make sure
you understand why they are there before you ignore them.

-- 
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
Note that posts from new members are moderated - please be patient with your 
first post.
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