How do nested dosync calls behave?
Asked Answered
F

1

14

What happens when you create nested dosync calls? Will sub-transactions be completed in the parent scope? Are these sub-transactions reversible if the parent transaction fails?

Fatherly answered 15/5, 2010 at 21:4 Comment(0)
B
18

If you mean syntactic nesting, then the answer is it depends on whether the inner dosync will run on the same thread as the outer one.

In Clojure, whenever a dosync block is entered, a new transaction is started if one hasn't been running already on this thread. This means that while execution stays on a single thread, inner transactions can be said to be subsumed by outer transactions; however if a dosync occupies a position syntactically nested within another dosync, but happens to be launched on a new thread, it will have a new transaction to itself.

An example which (hopefully) illustrates what happens:

user> (def r (ref 0))
#'user/r
user> (dosync (future (dosync (Thread/sleep 50) (println :foo) (alter r inc)))
              (println :bar)
              (alter r inc))
:bar
:foo
:foo
1
user> @r
2

The "inner" transaction retries after printing :foo; the "outer" transaction never needs to restart. (Note that after this happens, r's history chain is grown, so if the "large" dosync form were evaluated for a second time, the inner dosync would not retry. It still wouldn't be merged into the outer one, of course.)

Incidentally, Mark Volkmann has written a fantastic article on Clojure's Software Transactional Memory; it's highly recommended reading for anyone interested in gaining solid insight into details of this sort.

Bot answered 16/5, 2010 at 1:13 Comment(1)
Why the "syntactically" qualifier? And what does syntactically mean? I am a common lisper and I am accustomed to "lexically", as in closing over variables that are lexically visible: (let ((x 42)) (lambda () x)) vs dynamically (ie, bound in the call stack somewhere).Tehuantepec

© 2022 - 2024 — McMap. All rights reserved.