Transients are not thread safe. http://clojure.org/reference/transients

On Sunday, April 24, 2016, Vandr0iy <vandr0iy.perso...@gmail.com> wrote:

> Hi, Clojurists!
>
> I'm writing some clojure in these days, and I stumbled upon an error that
> I'm unable to easily understand.
> My goal is to asynchronously process a 2D vector in order to solve a
> parametric version of the 8 chess queens problem (where the parameters are:
> the board's dimensions and the chess pieces to be put on the board).
> My solution mainly consists in two functions: the worker and the boss. The
> worker takes in input a board (2d array) and yields a collection of boards
> that contain another chess piece placed in a certain way. This means that I:
> > take all of the free squares from the board
> > try to place the chesspiece on every one of them
> > if I succeed (do not hit other pieces) - it goes to the output.
>
> Until I use the version without the transients - it appears to be working.
> But I wanted to optimize my code, because the last time I solved it using
> recursive pmap - when I gave it an output that's a bit larger - it went
> into a bats**t crazy heap overflow. So I tried to use a transient
> collection.
> And here my problems began. When I try to execute this code it dies after
> just a couple of moves processed, giving me a nullpointer on the row where
> conj! is situated:
>
> NullPointerException   clojure.core/conj! (core.clj:3257)
>
> What a hellish issue may it be? Clojure is not helping - its error
> messages are always cryptic, and really hard to understand.
>
> Here is my code. It's not complete though - some functions are omitted,
> for the sake of brevity. Please, help me to understand what am I missing
> here...
>
> Thank you.
> (def fw (agent {}))
>
> (defn worker [subj]
>   (let [{brd :board pwns :pawns} subj
>         nxt (get-next-pawn pwns)]
>     (when (map? subj)
>       (when (and brd pwns)
>          (into #{}
>                (persistent!
>                 (reduce #(when (not (keyword? %2))
>                            (conj! %1
>                                   (if nxt
>                                     {:board %2 :pawns (update pwns nxt
> dec)}
>                                     brd)))
>                         (transient [])
>                         (map #(place nxt (first %) (last %) brd)
>                              (get-free-squares brd)))))))))
> (defn boss []
>
>       (send fw
>             #(into {} %2)
>             {:complete 0
>              :coll
>              #{{:board (blank-board 4 4)
>                 :pawns
>                 {:rook 1
>                  :knight 3
>                  :bishop 2
>                  }}}})
>          (loop []
>            (await fw)
>            @fw
>            (let [{done :complete col :coll} @fw]
>              (if (= done (count col))
>                col
>                (do (send-off fw
>                              #(identity {:complete (+ (:complete %1)
>                                                       (count (filter
> vector? %2)))
>                                          :coll (into (:coll %1) %2)})
>                              (reduce #(into %1 %2)
>                                      #{}
>                                      (pmap #(worker %) col)))
>                    (recur))))))
>
>
> --
> 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
> <javascript:_e(%7B%7D,'cvml','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
> <javascript:_e(%7B%7D,'cvml','clojure%2bunsubscr...@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
> <javascript:_e(%7B%7D,'cvml','clojure%2bunsubscr...@googlegroups.com');>.
> For more options, visit https://groups.google.com/d/optout.
>


-- 
“One of the main causes of the fall of the Roman Empire was that–lacking
zero–they had no way to indicate successful termination of their C
programs.”
(Robert Firth)

-- 
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/d/optout.

Reply via email to