On Wed, Jul 7, 2010 at 3:46 PM, Brian Hurt <bhur...@gmail.com> wrote:
> I'm wondering if the following pattern is safe or not.  I'm in a
> transaction, and I want to create an agent and then send it an initializing
> message (the message function isn't transaction-safe, so I don't want to run
> it in the transaction).  So I want to do something like:
>
> (def my-ref (ref nil))
>
> (defn example [ f ]
>     (dosync
>         (if (nil? @my-ref)
>             (let [ a (agent nil) ]
>                 (ref-set my-ref a)
>                 (send a init-function)
>                 (send a f))
>             (send @my-ref f))))
>
> (actually, what I want to do is rather more complicated than this, but this
> demonstrates the problem).  So, my question is: is it guaranteed that
> init-function will be received by the agent before any f functions are
> received?
>
> Brian
>
> --

It's not guaranteed the init-function will complete before f.

At the end of the Concurrency (Chapter 6) of Programming Clojure,
Stuart Halloway outlines a rationale and a way to create functions
that only run once.  Related code can be found here - see 'runonce':

http://github.com/stuarthalloway/lancet/blob/master/lancet.clj

I hope that helps with what you're trying to do!

Ryan

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