Timoteo
Timoteo

Reputation: 259

Counting aborted transactions in Clojure

I would like to analyze the behavior of a Clojure program versus a locking version in C. One metric I would like to track is the total number of aborted transactions for my Clojure program.

The only problem is that I can't mutate a variable outside of the context of the transaction that I am in. What I'm trying to do is this:

(dosync
(try
  (alter my_num inc)
  (catch Throwable t
    (do
      (alter total_aborts inc)
      (println "Caught " (.getClass t))
      (throw t)))))

Of course, total_aborts will never get incremented if the transaction doesn't finish!!! So how can I do this? Thanks!

Upvotes: 8

Views: 150

Answers (1)

Matthias Benkard
Matthias Benkard

Reputation: 15769

You can make my-num and total-aborts atoms and use swap! instead of alter.

Upvotes: 6

Related Questions