<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Clojure Q&amp;A - Recent activity in Refs, agents, atoms</title>
<link>https://ask.clojure.org/index.php/activity/clojure/refs-agents-atoms</link>
<description></description>
<item>
<title>Commented: Why vswap is a macro instead of function like swap?</title>
<link>https://ask.clojure.org/index.php/14531/why-vswap-is-a-macro-instead-of-function-like-swap?show=14535#c14535</link>
<description>with direct-linking enabled during compilation function will be called directly instead of resolving through a var , correct? (i am talking about direct function call , not passing function in HOF)</description>
<category>Refs, agents, atoms</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/14531/why-vswap-is-a-macro-instead-of-function-like-swap?show=14535#c14535</guid>
<pubDate>Tue, 06 May 2025 04:56:15 +0000</pubDate>
</item>
<item>
<title>Commented: Why does `await` fire off an `agent`'s watch?</title>
<link>https://ask.clojure.org/index.php/14357/why-does-await-fire-off-an-agent-s-watch?show=14361#c14361</link>
<description>sorry, overloaded term. In my use case specifically the agent sends a value to another agent. You can't await inside the agent action, and you can't await the first agent to wait for the chain to execute. So there is no way to wait for the second agent without explicitly tracking everything from the main thread.</description>
<category>Refs, agents, atoms</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/14357/why-does-await-fire-off-an-agent-s-watch?show=14361#c14361</guid>
<pubDate>Sat, 25 Jan 2025 11:01:52 +0000</pubDate>
</item>
<item>
<title>Edited: Is there a polished version of auto-agents?</title>
<link>https://ask.clojure.org/index.php/14318/is-there-a-polished-version-of-auto-agents?show=14318#q14318</link>
<description>&lt;p&gt;I was reading Clojure in Action and thinking about agents. It seems that by hooking up agents with watches, update functions, and some simple locks, you could create a “reactive&quot; state management. So that when some value &lt;code&gt;a&lt;/code&gt; is updated then all dependent state values update themselves automatically and in parallel on separate threads. While those values are updating you are free to change other state values that aren't in the same dependency graph&lt;/p&gt;
&lt;p&gt;I wrote up a quick draft but then started to look around online (it felt like a bit too obvious of an extension to the agent model). I found some similar ideas back in ~2009 but the trail goes dry. Is there some reason this isn't used more widely? Or is this some modern library I'm missing?&lt;/p&gt;
&lt;p&gt;There was a &quot;cell&quot; implementation by Stuart Sierra called &lt;code&gt;auto-agents&lt;/code&gt; - though I can't find the code&lt;/p&gt;
&lt;p&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://groups.google.com/g/clojure/c/NY834N34QvA&quot;&gt;https://groups.google.com/g/clojure/c/NY834N34QvA&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;I also found this complete implementation but it's also got no traction at all&lt;/p&gt;
&lt;p&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/apatil/lazy-agent&quot;&gt;https://github.com/apatil/lazy-agent&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Has this crystallized into a library somewhere? I'm thinking of writing my own implementation but I wanted a sanity check. I'm concerned maybe this idea was dropped for some reasons I've not considered&lt;/p&gt;
&lt;p&gt;(I'm aware there are things like Javelin, Odoyle and Missionary that can accomplish similar things.. but while they decouple code, they're way too complicated and some don't have a threading story)&lt;/p&gt;
&lt;p&gt;This is a copy of my question on Reddit:&lt;br&gt;
&lt;a rel=&quot;nofollow&quot; href=&quot;https://reddit.com/comments/1hklfyh/comment/m3gyyoq&quot;&gt;https://reddit.com/comments/1hklfyh/comment/m3gyyoq&lt;/a&gt;&lt;/p&gt;
</description>
<category>Refs, agents, atoms</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/14318/is-there-a-polished-version-of-auto-agents?show=14318#q14318</guid>
<pubDate>Thu, 26 Dec 2024 19:37:31 +0000</pubDate>
</item>
<item>
<title>Commented: Memory leak in seque via agents</title>
<link>https://ask.clojure.org/index.php/14185/memory-leak-in-seque-via-agents?show=14197#c14197</link>
<description>This leak could also affect overtone's ability to GC the cycled functions in this helper once the returned fn is GC'ed: &lt;a href=&quot;https://github.com/overtone/overtone/blob/535c9c50cb52aa275f3e2e7474842dbcfaa6eff5/src/overtone/algo/fn.clj#L27&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot;&gt;https://github.com/overtone/overtone/blob/535c9c50cb52aa275f3e2e7474842dbcfaa6eff5/src/overtone/algo/fn.clj#L27&lt;/a&gt;</description>
<category>Refs, agents, atoms</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/14185/memory-leak-in-seque-via-agents?show=14197#c14197</guid>
<pubDate>Tue, 15 Oct 2024 20:11:24 +0000</pubDate>
</item>
<item>
<title>Answer selected: Ref transactions causing deadlock at the REPL</title>
<link>https://ask.clojure.org/index.php/14016/ref-transactions-causing-deadlock-at-the-repl?show=14017#a14017</link>
<description>&lt;p&gt;I assume both transactions are failing and retrying multiple times, and each retry takes a very long time until one of them happens to get the timing right to work or maxes out the retries.&lt;/p&gt;
&lt;p&gt;You should never do blocking operations in your transactions.&lt;/p&gt;
</description>
<category>Refs, agents, atoms</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/14016/ref-transactions-causing-deadlock-at-the-repl?show=14017#a14017</guid>
<pubDate>Wed, 10 Jul 2024 15:49:14 +0000</pubDate>
</item>
<item>
<title>Answer selected: What's an ideomatic way to manage state in a library?</title>
<link>https://ask.clojure.org/index.php/13734/whats-an-ideomatic-way-to-manage-state-in-a-library?show=13738#a13738</link>
<description>&lt;p&gt;Yes, make state explicit, passed as arguments. Don't use global state. Don't use dynamic vars either. Don't assume a specific state framework such as Integrant or Component.&lt;/p&gt;
&lt;p&gt;This way consumers will have the most flexibility and simplicity.&lt;/p&gt;
</description>
<category>Refs, agents, atoms</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/13734/whats-an-ideomatic-way-to-manage-state-in-a-library?show=13738#a13738</guid>
<pubDate>Thu, 15 Feb 2024 13:57:30 +0000</pubDate>
</item>
<item>
<title>Edited: How to force a timeout?</title>
<link>https://ask.clojure.org/index.php/13545/how-to-force-a-timeout?show=13545#q13545</link>
<description>&lt;pre&gt;&lt;code&gt;(defn tid []                                                        
  [(.getName (Thread/currentThread))                                                        
   (.getId (Thread/currentThread))])

   (defn atomize                                                   
  [f data]                                                                                  
  (let [atomix (atom [])                                                                    
        pool (Executors/newFixedThreadPool 12)                                              
        tasks (map                                                                          
               (fn [datum]                                                                  
                 (fn []                                                                     
                   (swap! atomix conj [(f datum) (tid)])))                                  
               data)]                                                                       
    (doseq [future (.invokeAll pool tasks)]                                                 
      (.get future  10 TimeUnit/MILLISECONDS ))                                             
    (.shutdown pool)                                                                        
    @atomix))




I can't figure out where to place a `sleep` to force a timeout. 
&lt;/code&gt;&lt;/pre&gt;
</description>
<category>Refs, agents, atoms</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/13545/how-to-force-a-timeout?show=13545#q13545</guid>
<pubDate>Mon, 18 Dec 2023 17:48:00 +0000</pubDate>
</item>
<item>
<title>Comment reshown: Somehow I have to loop after all</title>
<link>https://ask.clojure.org/index.php/13479/somehow-i-have-to-loop-after-all?show=13488#c13488</link>
<description>Thanks. I begin to see it clearly now. And i really didn‘t see the race condition.</description>
<category>Refs, agents, atoms</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/13479/somehow-i-have-to-loop-after-all?show=13488#c13488</guid>
<pubDate>Wed, 22 Nov 2023 13:44:39 +0000</pubDate>
</item>
<item>
<title>Answered: How can I retrieve all older values of a PDS like atom/ref/agents ?</title>
<link>https://ask.clojure.org/index.php/11604/how-can-retrieve-all-older-values-of-pds-like-atom-ref-agents?show=11606#a11606</link>
<description>&lt;p&gt;I will add to the earlier answer. &lt;/p&gt;
&lt;p&gt;Clojure's reference types (atoms, ref, agents) are orthogonal to persistent data structures.  They are used in conjunction with persistent data structures (e.g. the expectation is that immutable values will be contained in said reference types to enforce software transactional memory semantics and the like), but they aren't directly related.  Reference types model an identity, e.g. some information that may vary over time where the successive values are related by some named thing.&lt;/p&gt;
&lt;p&gt;Persistent data structures let you maintain references to an original value, and create new values that represent semantic &quot;modifications&quot; to the original (e.g. associate a new key in map, change the value at index 3 in a vector, conj an item to a set, etc.).  Since this is accomplished via typically hash array mapped trie variants (and in the case of sorted collections, red/black balanced binary trees, or cons cells in the case of seqs), the efficiency gains come from sharing as much structure as possible between the derived &quot;new&quot; value and the old.  So some form of path copying scheme exists where only a minimal subset of the old tree is actually necessary to copy and then safely modify the copied subtree when creating the new structure; everything else is referenced.&lt;/p&gt;
&lt;p&gt;Aside from performance (minimal path copying in tries makes for surprisingly efficient vectors and maps), this enables writing in the pure functional style that clojure defaults to.  We can write complicated, useful programs without basing them on mutation and side effects.  Since our fundamental collections are persistent, then reasoning about the basic operations is trivial (pervasively so!):  I always know that assoc, dissoc, conj ,disj, etc. will not modify the input and will return either an equivalent value or a different value (as opposed to a mutable container where value/equality semantics are ambiguous).  Information flow through a program written in this style is distilled to functions, input, and output where information can only really flow one direction (since inputs are never modified).&lt;/p&gt;
&lt;p&gt;The other upside of this is that you have the ability to retain cheap (memory efficient) copies of older versions.  This is not intrinsic and has nothing to do with reference types directly, but we can use a reference type (like an atom) to implement some naive form of versioning:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(def versions (atom [[]]))
(defn current
  ([v] (peek @v))
  ([] (current versions)))
(defn update! [f &amp;amp; args]
  (let [newv (apply f (current) args)]
    (swap! versions conj newv)
    newv))

(defn roll-back!
  ([v] (-&amp;gt; v (swap! #(if (seq (peek %)) (pop %) %)) peek))
  ([] (roll-back! versions)))
&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;;; user=&amp;gt; (update! conj 2)
;; [2]
;; user=&amp;gt; (update! conj 3)
;; [2 3]
;; user=&amp;gt; (roll-back!)
;; [2]
;; user=&amp;gt; (roll-back!)
;; []

(let [original (current)
      v1 (update! conj 2)
      v2 (update! conj 3)
      v3 (update! conj 4)
      v4 (do (roll-back!) (roll-back!))]
  {:v1 v1
   :v2 v2
   :v3 v3
   :v4 v4})

;;{:v1 [2], :v2 [2 3], :v3 [2 3 4], :v4 [2]}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In the let demo, the references associated in the map for v1 v2 v3 and v4 are all different values, although there is structural sharing between them.  They are all thread safe and can persist without affecting anything else (since they can't change).  Assuming space isn't a concern, using this kind of scheme allows trivial &quot;undo&quot; at potentially complex application data levels.  It is trivial to maintain everything in an app state, and retain a log of previous versions.  Rewinding the application state is as simple as popping items off of the history.  We don't need to delta diff or anything, or generate patches, since the shared structure is already taken care of for us.&lt;/p&gt;
&lt;p&gt;If you stick with the persistent data structures to model your application data, then you effectively have the ability to snapshot the entire &quot;world&quot; at whatever level of granularity you would like (if it makes senses).&lt;/p&gt;
</description>
<category>Refs, agents, atoms</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/11604/how-can-retrieve-all-older-values-of-pds-like-atom-ref-agents?show=11606#a11606</guid>
<pubDate>Mon, 28 Feb 2022 13:23:12 +0000</pubDate>
</item>
<item>
<title>Closed: calling realized? on a delay which is currently executing blocks the caller</title>
<link>https://ask.clojure.org/index.php/10004/calling-realized-delay-currently-executing-blocks-caller?show=10004#q10004</link>
<description>&lt;p&gt;If a &lt;code&gt;delay&lt;/code&gt; is currently processing, calling &lt;code&gt;realized?&lt;/code&gt; causes the caller to block until the delay has completed and then returns true.&lt;/p&gt;
&lt;p&gt;The behaviour I would have expected is that it should return false, which is what happens with futures. After all, if you wanted to wait until it was completed, you would &lt;code&gt;deref&lt;/code&gt; it.&lt;/p&gt;
&lt;p&gt;(also there don't seem to be any tags or categories related to concurrent programming, but since delay is part of clojure.core, I assume this is the correct place to ask)&lt;/p&gt;
</description>
<category>Refs, agents, atoms</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/10004/calling-realized-delay-currently-executing-blocks-caller?show=10004#q10004</guid>
<pubDate>Tue, 14 Sep 2021 21:30:54 +0000</pubDate>
</item>
<item>
<title>Answer selected: How to known all the actions of an agent have been executed?</title>
<link>https://ask.clojure.org/index.php/10000/how-to-known-all-the-actions-of-an-agent-have-been-executed?show=10001#a10001</link>
<description>&lt;p&gt;&lt;code&gt;await&lt;/code&gt; does exactly this:&lt;br&gt;
&lt;code&gt;(clojure.repl/doc await)&lt;/code&gt;&lt;br&gt;
&lt;code&gt;clojure.core/await
([&amp;amp; agents])
  Blocks the current thread (indefinitely!) until all actions
  dispatched thus far, from this thread or agent, to the agent(s) have
  occurred.  Will block on failed agents.  Will never return if
  a failed agent is restarted with :clear-actions true or shutdown-agents was called.&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;To try it out:&lt;br&gt;
&lt;code&gt;(def a (agent 0))
(dotimes [_ 100] (send-off a (fn [x] (Thread/sleep 30) (inc x))))
(await a)&lt;/code&gt;&lt;br&gt;
(Will block for a few seconds)&lt;/p&gt;
</description>
<category>Refs, agents, atoms</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/10000/how-to-known-all-the-actions-of-an-agent-have-been-executed?show=10001#a10001</guid>
<pubDate>Tue, 05 Jan 2021 07:53:30 +0000</pubDate>
</item>
<item>
<title>Answered: Where is volatile! officially documented?</title>
<link>https://ask.clojure.org/index.php/9323/where-is-volatile-officially-documented?show=9324#a9324</link>
<description>&lt;p&gt;Clojure’s use does build on Java’s volatiles so the name is intentional.&lt;/p&gt;
&lt;p&gt;The only other place I can think of where it’s documented is in the release notes &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/clojure/clojure/blob/master/changes.md#21-transducers&quot;&gt;https://github.com/clojure/clojure/blob/master/changes.md#21-transducers&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;It’s considered kind of an advanced tool so it’s intentionally not front and center. Could probably be mentioned in the transducer docs as an impl tool.&lt;/p&gt;
</description>
<category>Refs, agents, atoms</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/9323/where-is-volatile-officially-documented?show=9324#a9324</guid>
<pubDate>Sat, 23 May 2020 23:36:32 +0000</pubDate>
</item>
<item>
<title>Answered: atom watchers are not atomic with respect to reset!</title>
<link>https://ask.clojure.org/index.php/3816/atom-watchers-are-not-atomic-with-respect-to-reset?show=4043#a4043</link>
<description>Reference: &lt;a href=&quot;https://clojure.atlassian.net/browse/CLJ-1770&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot;&gt;https://clojure.atlassian.net/browse/CLJ-1770&lt;/a&gt; (reported by ericnormand)</description>
<category>Refs, agents, atoms</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/3816/atom-watchers-are-not-atomic-with-respect-to-reset?show=4043#a4043</guid>
<pubDate>Wed, 26 Jun 2019 12:00:00 +0000</pubDate>
</item>
<item>
<title>Answered: Tests for clojure.core/atom</title>
<link>https://ask.clojure.org/index.php/2407/tests-for-clojure-core-atom?show=2690#a2690</link>
<description>Reference: &lt;a href=&quot;https://clojure.atlassian.net/browse/CLJ-1915&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot;&gt;https://clojure.atlassian.net/browse/CLJ-1915&lt;/a&gt; (reported by claj)</description>
<category>Refs, agents, atoms</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/2407/tests-for-clojure-core-atom?show=2690#a2690</guid>
<pubDate>Wed, 26 Jun 2019 12:00:00 +0000</pubDate>
</item>
</channel>
</rss>