Guest User

Untitled

a guest
May 7th, 2010
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 3.83 KB | None | 0 0
  1. ; The following code has no memory leak
  2. (defn start-handler [[nme definition]]                                                                                                                                                                    
  3.   (let [queue (new LinkedBlockingQueue)                                                                                                                                                                  
  4.         handler (:handler definition)                                                                                                                                                                    
  5.         condition (:cond definition)                                                                                                                                                                      
  6.         name (str "handler" nme)]                                                                                                                                                                        
  7.     (println "starting" name)                                                                                                                                                                            
  8.     (start-thread #(handler queue) name)                                                                                                                                                                  
  9.     #(if (condition %) (.put queue %))))
  10.  
  11. (defn filter-with-date [e]                                                                                                                                                                                
  12.   (not                                                                                                                                                                                                    
  13.     (or (nil? (:date e))                                                                                                                                                                                  
  14.         (= (:name e) "-"))))                                                                                                                                                                              
  15.                                                                                                                                                                                                          
  16. (defn printer []                                                                                                                                                                                          
  17.   (struct-map handler                                                                                                                                                                                    
  18.     :cond filter-with-date                                                                                                                                                                                
  19.     :handler (fn [queue]                                                                                                                                                                                  
  20.                (loop []                                                                                                                                                                                  
  21.                  (println (.take queue))                                                                                                                                                                  
  22.                  (recur)))))
  23.  
  24. ; Another thread does
  25. (start-handler [:print-handler (printer)])
  26. ; and then pushes stuff into the LBQ using the returned function
Advertisement
Add Comment
Please, Sign In to add comment