4.86. kink/thread/THREAD

Provides access to preemptive threads.

4.86.1. THREAD.spawn($thunk)

`spawn` starts a new preemptive thread, and runs $thunk in the thread.


• $thunk must be a fun which takes no arg

This fun returns nada.

Living threads do not prevent the process from exiting, thus they act like Java's daemon threads.

When you spawn a thread, it is automatically detatched from the spawning thread, thus there is no need to join the thread. If you want to block until another thread completes its work, use a concurrency primitive such as MUTEX.


• Invocation of `spawn` happens before invocation of $thunk in the new thread.

4.86.2. THREAD.sleep(Seconds)

`sleep` causes the current thread to sleep for the specified `Seconds`.

`Seconds` must be a non-negative num of arbitrary precision. The maximum precision effective in the result depends on the runtime system. For example, in a Kink runtime system on the JVM, the two invocations of STOPWATCH.start in the following program are equivalent. Both tries to sleep 1234 milliseconds.

THREAD.sleep(1.23456) # tries to sleep 1234 milliseconds
THREAD.sleep(1.234)   # tries to sleep 1234 milliseconds