Created
July 24, 2013 18:57
-
-
Save halgari/6073419 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns hoare.problem | |
(:require [clojure.core.async :refer :all])) | |
(defn fan-in | |
([ins] (fan-in (chan) ins)) | |
([c ins] | |
(go (while true | |
(let [[x] (alts! ins)] | |
(>! c x)))) | |
c)) | |
(defn service [] | |
(let [in (chan) | |
out (chan)] | |
(go (while true | |
(let [val (<! in)] | |
(>! out (str val " done"))))) | |
[in out])) | |
;; On the console I see messages coming from c | |
;; that have not been modified by the service; | |
;; furthermore they're coming out of order, | |
;; such as "b 9" before "b 8 done". | |
;; Don't know what's GO ing on here. | |
(defn test-fan-in [] | |
(let [[a-in a-out] (service) | |
[b-in b-out] (service) | |
c (fan-in [a-out b-out])] | |
(go (while true | |
(println "c:" (<! c)))) | |
(go (dotimes [i 10] | |
(>! a-in (str "a " i)))) | |
(go (dotimes [i 10] | |
(>! b-n (str "b " i))))) | |
nil) | |
; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment