Quirrel4.41.0

newthread

Global, available without an import
newthread(func: function): thread

Creates a thread (a coroutine) that runs func when resumed.

Parameters

funcfunctiona script closure to run on the new thread

Return value

The new thread, suspended before its first instruction. Call thread.call(...) on it to start func, and thread.wakeup(...) to resume it after a suspend inside func.

Errors

Throws newthread expects a non-native closure for a native function such as print: only script closures can run on a thread of their own.

Example

examples/globals/newthread.nut
let co = newthread(function() {
  println("first")
  let x = suspend("paused")     // pauses the thread and hands "paused" to the caller
  println("resumed with", x)
})

println("co.call() =", co.call())          // runs until suspend(), returns suspend's argument
println("co.wakeup(\"hello\") =", co.wakeup("hello")) // resumes; suspend() returns "hello" inside the thread
println("co.getstatus() =", co.getstatus())
Output:
first
co.call() = paused
resumed with hello
co.wakeup("hello") = null
co.getstatus() = idle

See also

suspendPauses the running thread and hands its arguments to whoever resumes it.
globalsmodule index