Sounds to me like you should use an agent for this. 

Don't worry about the overhead prematurely, it's actually pretty minimal. 
Just make sure you choose between "send" and "send-off" appropriately (i.e. 
does your further processing block or not?)

On Friday, 20 September 2013 00:20:11 UTC+8, Adam Clements 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.

Reply via email to