newthread
Creates a thread (a coroutine) that runs func when resumed.
Parameters
func | function | a 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
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() = idleSee also
suspend | Pauses the running thread and hands its arguments to whoever resumes it. |
| globals | module index |