Last active
August 27, 2021 07:25
-
-
Save sogaiu/f729946137808db1b0413f25fb85b891 to your computer and use it in GitHub Desktop.
thread channels with supervisor
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
# channels that can be used for communication between os threads | |
(def t-chan-a (ev/thread-chan 10)) | |
(def t-chan-b (ev/thread-chan 10)) | |
# supervisor channel | |
(def t-sv-chan (ev/thread-chan 10)) | |
# one thread | |
(ev/thread | |
(fiber/new | |
(fn [x] | |
(printf "thread 1 was passed: %p" x) | |
# | |
(ev/give-supervisor x) | |
(ev/sleep 1) | |
(def msg "breathe in") | |
(print "thread 1 sending: " msg) | |
(ev/give t-chan-a msg) | |
# | |
(print "thread 1 received: " (ev/take t-chan-b)) | |
x)) | |
:1 | |
:n | |
t-sv-chan) | |
# another thread | |
(ev/thread | |
(fiber/new | |
(fn [x] | |
(printf "thread 2 was passed: %p" x) | |
# | |
(ev/give-supervisor x) | |
(print "thread 2 received: " (ev/take t-chan-a)) | |
# | |
(def msg "breathe out") | |
(print "thread 2 sending: " msg) | |
(ev/give t-chan-b msg) | |
x)) | |
:2 | |
:n | |
t-sv-chan) | |
# watch for things on the supervisor channel | |
(ev/thread | |
(fiber/new | |
(fn [] | |
(def [contact] (ev/take t-sv-chan)) | |
(printf "%p started" contact) | |
(def [contact] (ev/take t-sv-chan)) | |
(printf "%p started" contact) | |
(for i 0 2 | |
(def [result f] (ev/take t-sv-chan)) | |
(if (= result :ok) | |
(printf "%p finished ok with: %p" f (fiber/last-value f)) | |
(printf "things didn't go so well for: %p" f))) | |
# | |
(print "that's all folks!")))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment