2019-12-02 02:47:22 +00:00
|
|
|
(defn worker-main
|
2019-12-06 07:46:23 +00:00
|
|
|
"Sends 11 messages back to parent"
|
2019-12-02 02:47:22 +00:00
|
|
|
[parent]
|
2019-12-07 22:51:00 +00:00
|
|
|
(def name (thread/receive))
|
|
|
|
(def interval (thread/receive))
|
2019-12-02 02:47:22 +00:00
|
|
|
(for i 0 10
|
2019-12-02 10:39:13 +00:00
|
|
|
(os/sleep interval)
|
2019-12-06 07:46:23 +00:00
|
|
|
(:send parent (string/format "thread %s wakeup no. %d" name i)))
|
2019-12-05 04:31:01 +00:00
|
|
|
(:send parent name))
|
2019-12-02 02:47:22 +00:00
|
|
|
|
|
|
|
(defn make-worker
|
2019-12-02 10:39:13 +00:00
|
|
|
[name interval]
|
2019-12-18 20:07:46 +00:00
|
|
|
(-> (thread/new worker-main)
|
2019-12-02 10:39:13 +00:00
|
|
|
(:send name)
|
|
|
|
(:send interval)))
|
2019-12-02 02:47:22 +00:00
|
|
|
|
2019-12-06 07:46:23 +00:00
|
|
|
(def bob (make-worker "bob" 0.02))
|
|
|
|
(def joe (make-worker "joe" 0.03))
|
|
|
|
(def sam (make-worker "sam" 0.05))
|
|
|
|
|
2019-12-02 10:39:13 +00:00
|
|
|
# Receive out of order
|
2019-12-07 22:51:00 +00:00
|
|
|
(for i 0 33
|
|
|
|
(print (thread/receive)))
|
2019-12-06 07:46:23 +00:00
|
|
|
|
|
|
|
#
|
|
|
|
# Recursive Thread Tree - should pause for a bit, and then print a cool zigzag.
|
|
|
|
#
|
|
|
|
|
|
|
|
(def rng (math/rng (os/cryptorand 16)))
|
|
|
|
|
|
|
|
(defn choose [& xs]
|
|
|
|
(in xs (:int rng (length xs))))
|
|
|
|
|
|
|
|
(defn worker-tree
|
|
|
|
[parent]
|
2019-12-07 22:51:00 +00:00
|
|
|
(def name (thread/receive))
|
|
|
|
(def depth (thread/receive))
|
2019-12-06 07:46:23 +00:00
|
|
|
(if (< depth 5)
|
|
|
|
(do
|
|
|
|
(defn subtree []
|
2019-12-18 20:07:46 +00:00
|
|
|
(-> (thread/new worker-tree)
|
2019-12-06 07:46:23 +00:00
|
|
|
(:send (string name "/" (choose "bob" "marley" "harry" "suki" "anna" "yu")))
|
|
|
|
(:send (inc depth))))
|
|
|
|
(let [l (subtree)
|
|
|
|
r (subtree)
|
2019-12-07 22:51:00 +00:00
|
|
|
lrep (thread/receive)
|
|
|
|
rrep (thread/receive)]
|
2019-12-06 07:46:23 +00:00
|
|
|
(:send parent [name ;lrep ;rrep])))
|
|
|
|
(do
|
|
|
|
(:send parent [name]))))
|
|
|
|
|
2019-12-18 20:07:46 +00:00
|
|
|
(-> (thread/new worker-tree) (:send "adam") (:send 0))
|
2019-12-07 22:51:00 +00:00
|
|
|
(def lines (thread/receive))
|
2019-12-06 07:46:23 +00:00
|
|
|
(map print lines)
|
2019-12-06 15:21:36 +00:00
|
|
|
|
|
|
|
#
|
|
|
|
# Receive timeout
|
|
|
|
#
|
|
|
|
|
2019-12-07 22:51:00 +00:00
|
|
|
(def slow (make-worker "slow-loras" 0.5))
|
2019-12-06 15:21:36 +00:00
|
|
|
(for i 0 50
|
|
|
|
(try
|
2019-12-07 22:51:00 +00:00
|
|
|
(let [msg (thread/receive 0.1)]
|
2019-12-06 15:21:36 +00:00
|
|
|
(print "\n" msg))
|
|
|
|
([err] (prin ".") (:flush stdout))))
|
|
|
|
|
|
|
|
(print "\ndone timing, timeouts ending.")
|
2019-12-07 22:51:00 +00:00
|
|
|
(try (while true (print (thread/receive))) ([err] (print "done")))
|