I am familiarizing myself w/ Clojure with Stuart Holloway's book,
"Programming Clojure".  A great introduction to Clojure - very
helpful.

ASIDE - this actually makes me wonder...are add'l books about Clojure
currently being written?  If nobody else knows, RH probably would.

While reading the "Prefer alter" section of Ch. 6, "Concurrrency", it
struck me that the author fails to address the most serious error that
can occur when 'commute' is used where 'alter' should have been used.
So I am writing to share my "discovery" as warning to others; and to
obtain feedback/correction in case I am missing something.

This paragraph is the thesis of that section.

         "You should not use 'commute' to update the counter.
         'commute' returns the in-transaction value of the counter at
         the time of the 'commute', but reorderings could cause the
         actual end-of-transaction value to be different.  This could
         lead to more than one caller getting the same counter value."

IOW, the primary risk w/ using 'commute' is the unreliability of its
return-value b/c of Clojure's idiomatic use of the return-value.  I
suggest that associating the risk w/ an idiom is a weak reason
especially since the mutators are, by definition, non-functional.  It
is true but is easily fixed by using a different idiom or even a
macro.

However, that is only my opinion - and we all know the value of an
opinion.  And it is not my primary concern.

My primary concern is that for certain kinds of transactions, use of
'commute' creates a race condition between transactions that operate
on a common set (non-null) of Refs.

What kinds of transactions?  Transactions that use the in-transaction
value of Ref in its operations.  This includes:

        * setting Ref's state based on its current state.
        * setting the state of anything else based on the Ref's
          current state.
        * executing operations depending on the Ref's current value.

Essentially, any operation that depends on visibility into the Ref's
current value will be affected by the race condition.  As most of us
know, such a race condition can cause a lot of trouble and be very
difficult to characterize.

I surmised from this description that it is idiomatic in Clojure to
use the return-value of an invocation of 'alter', 'commute' and 'set-
ref'.  I do not KNOW since I am just now experimenting w/ Clojure for
potential use on a project.

-robert

--~--~---------~--~----~------------~-------~--~----~
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