Skip to content

Instantly share code, notes, and snippets.

@bakpakin
Created August 27, 2021 13:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bakpakin/f9d59ac184c2ad1930e81ef3bd08b3a7 to your computer and use it in GitHub Desktop.
Save bakpakin/f9d59ac184c2ad1930e81ef3bd08b3a7 to your computer and use it in GitHub Desktop.
Try (and fail) to repro thread issue.
(defn parallel
[jobs]
(print "Beginning " (length jobs) " jobs...")
(let [results-chan (ev/thread-chan 0)
res @{}]
(each job jobs
(ev/thread (fiber/new job) results-chan :n))
(while (< (length res) (length jobs))
(let [[k v] (ev/take results-chan)]
(print "job " k " finished")
(put res k v)))
(print "Finished jobs.")
res))
(defn randsleep
[]
(math/seedrandom (os/cryptorand 16))
(os/sleep (math/random)))
(def jobs
[(fn [x] (randsleep) (ev/give x [:a "A"]))
(fn [x] (randsleep) (ev/give x [:b "B"]))
(fn [x] (randsleep) (ev/give x [:c stdout]))
(fn [x] (randsleep) (ev/give x [:d "D"]))])
(pp (parallel jobs))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment