Skip to content

Instantly share code, notes, and snippets.

@slipset
Created December 29, 2014 08:55
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 slipset/fb7d2a44303481731d89 to your computer and use it in GitHub Desktop.
Save slipset/fb7d2a44303481731d89 to your computer and use it in GitHub Desktop.
Implementation of the barbershop problem with core.async
(ns barbershop.core
(:require [clojure.core.async :refer
[dropping-buffer timeout chan go-loop <! chan ]]))
(def shop (chan (dropping-buffer 3)))
(defn customer [i shop]
(put! shop i (fn [v] (println "customer" i "going to the barber"))))
(defn barber [shop]
(go-loop []
(let [v (<! shop)]
(println "cutting hair of customer " v)
(<! (timeout (+ 100 (rand-int 600)))))
(recur)))
(barber shop)
(doseq [n (range 1 200)]
(Thread/sleep (+ 100 (rand-int 200)))
(customer n shop))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment