1
0
mirror of https://github.com/janet-lang/janet synced 2025-10-06 11:32:29 +00:00

Add methods to threads.

This commit is contained in:
Calvin Rose
2019-12-02 04:39:13 -06:00
parent 70c8b6838d
commit 474bcd50a1
2 changed files with 32 additions and 12 deletions

View File

@@ -1,18 +1,24 @@
(defn worker-main
[parent]
(def name (thread/receive parent))
(def name (:receive parent))
(def interval (:receive parent))
(for i 0 10
(os/sleep 1)
(print "thread " name " wakeup no. " i))
(thread/send parent :done))
(os/sleep interval)
(printf "thread %s wakeup no. %d\n" name i))
(:send parent :done))
(defn make-worker
[name]
(-> (thread/new) (thread/send worker-main) (thread/send name)))
[name interval]
(-> (thread/new)
(:send worker-main)
(:send name)
(:send interval)))
(def bob (make-worker "bob"))
(os/sleep 0.5)
(def joe (make-worker "joe"))
(def bob (make-worker "bob" 0.2))
(def joe (make-worker "joe" 0.3))
(def sam (make-worker "joe" 0.5))
(thread/receive bob)
(thread/receive joe)
# Receive out of order
(:receive bob)
(:receive sam)
(:receive joe)