On 8 Mrz., 13:40, Mark Volkmann <[email protected]> wrote:
> Is there a webpage or video that describes what Clojure transactions
> do to avoid deadlocks? I'm not having a particular issue. I just want
> to understand what is provided. Perhaps I just need to look at the
> source starting from dosync.

Hi Mark.
I understand it this way:
alter/commute/ref-set will immediately make the change in a protected
area of the data structure, without caring about locks. These changes
are invisible to users of the data. This is called an optimistic
strategy. When the changes are written the transaction checks if the
data was touched by another thread. If this should be the case it
throws away the changes it made and restarts from scratch.
If no change was made, then Clojure will lock the data for a millionth
of a second and make the changes go live.
Clojure is semi optimistic, which means, that it will not do tons of
changes and at the end of the transaction check if a rollback is
needed.
Instead it checks several times during a transaction if it still makes
sense to continue. If some other thread already made a change, then
the
current transaction can be aborted and gets automatically restarted.

There is a limit of 10.000 restarts I think.
This way a deadlock can not occur, but a livelock instead.
If you have a BIG transaction that needs to do lots of stuff, and if
you
have maany threads hammering on that data with a transaction, then it
is
possible that no one (= no thread) can finish. It just takes too much
time
and no thread is able to complete the full transaction before some
other
thread had a chance to change anything.
In that case the user should use a lock, which is a very simple thing
to
do in Clojure:  (lock ...).
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to