You can use `ensure` if you want to make sure that the transaction
fails if a or b are altered while the threads are sleeping in your
example:

http://clojure.org/api#ensure

Otherwise, regarding serializable concurrency level: If you want
mutual exclusion, then that's what locks are for.

On Thu, Jun 18, 2009 at 8:38 AM, Eric Willigers<[email protected]> wrote:
>
> Hi all,
>
> I'm enjoying using Clojure. It is clean, concise and powerful.
>
> The containers and even defmacro are easy to use correctly. However,
> I'm yet to be convinced the same applies with the STM implementation.
>
> In the user code below, people might expect  b == a + 1  after the two
> transactions run. Unfortunately, that isn't the case, as no read locks
> are being taken. (It would have been true if the transactions ran
> sequentially, in either order.)
>
> Is the lack of isolation affecting people, or do most users use idioms
> that avoid the problem?
> Example idioms would be writing every ref that is read, or using a
> single ref to switch to a new immutable data structure (as opposed to
> implementing mutable data structures using numerous refs).
>
> Regards,
> Eric.
>
> P.S. I'm aware Oracle and PostgreSQL similarly have non-serializable
> transactions.
>
>
>
> (def a (ref 0))
> (def b (ref 0))
>
> (.start
>  (Thread.
>    #(dosync
>      (let [old-a @a]
>        (Thread/sleep 2000) ; or any slow computation
>        (ref-set b (inc old-a))))))
>
> (.start
>  (Thread.
>    #(dosync
>      (let [old-b @b]
>        (Thread/sleep 2000) ; or any slow computation
>        (ref-set a (dec old-b))))))
>
> (Thread/sleep 6000) ; or join the above threads
> (println "a=" @a ", b=" @b)
>
>      a= -1 , b= 1
>
> >
>



-- 
Venlig hilsen / Kind regards,
Christian Vest Hansen.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your 
first post.
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