JanetDocsPlaygroundI'm feeling luckyGitHub sign in

ev/thread



    cfunction
    src/core/ev.c on line 2894, column 1

    (ev/thread main &opt value flags supervisor)

    Run `main` in a new operating system thread, optionally passing 
    `value` to resume with. The parameter `main` can either be a fiber, 
    or a function that accepts 0 or 1 arguments. Unlike `ev/go`, this 
    function will suspend the current fiber until the thread is 
    complete. If you want to run the thread without waiting for a 
    result, pass the `:n` flag to return nil immediately. Otherwise, 
    returns nil. Available flags:

    * `:n` - return immediately
    * `:t` - set the task-id of the new thread to value. The task-id is 
      passed in messages to the supervisor channel.
    * `:a` - don't copy abstract registry to new thread (performance 
      optimization)
    * `:c` - don't copy cfunction registry to new thread (performance 
      optimization)


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