I am writing a Mandelbrot Set , and to speed up figuring out which points go to infinity, I decided to try a ExecutorService to check for parallel points.
Basically, the plan is as follows:
- Calculate all the points I need to find
- Specify each service point
- Ask the service to wrap your results in an
Atom a vector wrapper as you create them. - Periodically create a drawing code to get the results and clear the queue
My problem is with the last point. How can I safely capture previous results from an atom and reset it?
I thought of a simple way simply:
(def draw-queue-A (atom [])) (defn grab-and-clear-queue [] (let [results @draw-queue-A] (reset! draw-queue-A []) results))
But it does not look safe. If between dereferencing and reset! something added, it will be lost.
The ominous abomination that I stopped at the moment:
(defn grab-and-clear-queue [] (let [results (atom [])] (swap! draw-queue-A (fn [res] (reset! results res) [])) results))
But using an atom just to get results seems ridiculous.
How can I safely extract the contents of an atom and reset without losing any results?
source share