Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (ns clj-project.barrier)
- (declare thread-factory )
- (declare thread-factory-looping)
- (defn run-with-barrier-looping
- "Run the body function number times in seperate threads.
- Each thread will be listen to a barrier before executing his task.
- We return the atom that will stop these threads."
- [body number]
- (let [start? (atom false)
- stop (atom false)
- indices (map list (iterate inc 0)) ; Returns (1 2 3..)
- creators (repeat number #(thread-factory-looping % body start? stop)) ; Returns a list of partials
- threadfunctions (map apply creators indices) ; Should return a list of functions
- threads (map #(Thread. %) threadfunctions)] ; Thread all the functions!
- (doseq [t threads] (.start t))
- (reset! start? true)
- (reset! stop true) ;; works here!
- (doseq [t threads] (.join t))
- stop)) ;; stops working here..
- (defn thread-factory-looping
- "Creates a thread which has a unique id.
- It loops each time."
- [thread-id body start? stop]
- (fn []
- (loop [go? @start?]
- (if go?
- (do
- (body thread-id)
- (if (not @stop)
- (do (println @stop)
- (recur true))))
- (recur @start?)))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement