On 21 February 2010 09:24, ataggart <alex.tagg...@gmail.com> wrote:
> On Feb 20, 6:07 am, Jack Fang <jack.fang1...@gmail.com> wrote:
>>    Suppose the transfer-money and print-info are run in different
>> thread. With the current Clojure STM, is it possible that print-info
>> prints the old source-account information and modified dest-account
>> information?
>
> Yes.
>
>> Do I need to add a dosync to the print-info function? I
>> read the Programming Clojure. It says do not have side-effect on a
>> transaction.
>
> If you want consistent results between refs, you'll need to be in a
> transaction, thus dosync.  You might also need to use ensure, but I'm
> not certain.  The reason side-effects are discouraged (not forbidden)
> is because a transaction may be run multiple times, thus you might get
> multiple printouts.
>
>> If I add dosync to the print-info, do I need to use agent
>
> Agents are desgned to be used for side-effects in a transaction.
> That's why sends to an agent are held and only actually happen if the
> tx commits.  It mIght be overkill for your examle, but yes send
> ensured/altered values to an agent from inside a dosync block.
>> to print the info?

But surely in this case you could do:

(defn print-info []
  (let [[src dst] (dosync [...@source-account @dest-account])]
    (println src)
    (println dst)))

or maybe:

(defn print-info []
  (let [[src dst] (dosync
                    (ensure source-account)
                    (ensure dest-account)
                    [...@source-account @dest-account])]
    (println src)
    (println dst)))

I suspect the ensures are necessary, but I would appreciate it if
someone could explain why they are (or are not) necessary in this
case.

Thanks.

-- 
Michael Wood <esiot...@gmail.com>

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