JanetDocsPlaygroundI'm feeling luckyGitHub sign in

ev/thread



    cfunction

    (ev/thread fiber &opt value flags)

    Resume a (copy of a) `fiber` in a new operating system thread, 
    optionally passing `value` to resume with. Unlike `ev/go`, this 
    function will suspend the current fiber until the thread is 
    complete. The the final result.


1 exampleSign in to add an example
Loading...
# channels that can be used for communication between os threads
(def chan-a (ev/thread-chan 10))
(def chan-b (ev/thread-chan 10))

# one thread
(ev/thread
  (fiber/new
    (fn []
      (def msg "hi")
      (print "thread 1 sending: " msg)
      (ev/give chan-a msg)
      #
      (print "thread 1 received: " (ev/take chan-b))))
  :1
  :n)

# another thread
(ev/thread
  (fiber/new
    (fn []
      (print "thread 2 received: " (ev/take chan-a))
      #
      (def msg "peace")
      (print "thread 2 sending: " msg)
      (ev/give chan-b msg)))
  :2
  :n)

# expected output
#
# thread 1 sending: hi
# thread 2 received: hi
# thread 2 sending: peace
# thread 1 received: peace
sogaiuPlayground