Skip to content

Instantly share code, notes, and snippets.

@gorsuch
Created January 24, 2013 03:09
Show Gist options
  • Save gorsuch/4617330 to your computer and use it in GitHub Desktop.
Save gorsuch/4617330 to your computer and use it in GitHub Desktop.
queue notes for clojure

Taken from here, recording so I don't lose it:

(defn new-q [] (java.util.concurrent.LinkedBlockingDeque.))

(defn offer! 
  "adds x to the back of queue q"
  [q x] (.offer q x) q)

(defn take! 
  "takes from the front of queue q.  blocks if q is empty"
  [q] (.take q))
(def example-queue (new-q))

;; background thread, blocks waiting for something to print
(future (println ">> " (take! example-queue))) 

;; the future-d thread will be unblocked and ">> HELLO WORLD" is printed.
(offer! example-queue "HELLO WORLD") 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment