It's possible with a ref, but I don't need to coordinate multiple bits of
state so a ref feels like overkill, and it makes adding to the list far
more cumbersome as it needs to be inside a dosync, and I essentially end up
writing atom-like functions to make that easier... by which point I may as
well have used an atom and my function.

Are refs faster? I guess I could add to my list commutatively which might
give some slight benefit, depending how many threads are bashing it. But I
still think there's value in being able to retrieve the old value of an
atom as part of a reset! type operation.

Adam


Adam Clements

+44 7947 724 795
--
This email and any files transmitted with it are confidential. If you are
not the intended recipient, you are hereby notified that any disclosure,
distribution or copying of this communication is strictly prohibited.


On Thu, Sep 19, 2013 at 5:21 PM, Ben Wolfson <wolf...@gmail.com> wrote:

> Is there some reason you can't use a ref for this?
>
>
> On Thu, Sep 19, 2013 at 9:20 AM, Adam Clements <adam.cleme...@gmail.com>wrote:
>
>> Hi,
>>
>> I have been working on a setup where I batch a number of updates in a
>> queue, which I store in an atom with multiple threads potentially adding
>> things to it. Periodically I want to flush that queue, leaving an empty
>> list in the atom and passing the current batch of values on for further
>> processing.
>>
>> I can't use swap! for this, because my further processing is side
>> effecting and I wouldn't want to dispatch the same information twice, and I
>> can't use reset! because that just throws away the old contents of the
>> atom, and derefing it in the line before I am likely to lose data if
>> another thread adds something at the last minute.
>>
>> I could change to an agent with append and flush actions which would get
>> queued up and run only once each, and that would work. But it has quite a
>> lot of thread overhead which I think is unnecessary.
>>
>> What I ended up doing was writing a version of reset! which returns the
>> old value instead of the new value (why would I want the new value? I know
>> that, I put it in!)
>>
>> (defn flush! [atom newval]
>>    (let [val @atom]
>>       (if (compare-and-set! atom val newval)
>>          val
>>          (recur atom newval))))
>>
>> => (def a (atom [1 2 3 4]))
>> #'user/a
>> => (flush! a [])
>> [1 2 3 4]
>> => @a
>> []
>>
>> Is there already a standard library fn I should be using for this? Is
>> this a bad idea for some reason I don't fathom?
>>
>> Adam
>>
>> https://github.com/AdamClements
>>
>> --
>> --
>> 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
>> ---
>> You received this message because you are subscribed to the Google Groups
>> "Clojure" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to clojure+unsubscr...@googlegroups.com.
>> For more options, visit https://groups.google.com/groups/opt_out.
>>
>
>
>
> --
> Ben Wolfson
> "Human kind has used its intelligence to vary the flavour of drinks, which
> may be sweet, aromatic, fermented or spirit-based. ... Family and social
> life also offer numerous other occasions to consume drinks for pleasure."
> [Larousse, "Drink" entry]
>
>  --
> --
> 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
> ---
> You received this message because you are subscribed to the Google Groups
> "Clojure" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to clojure+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>

-- 
-- 
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
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to