Skip to content

threads

import threads

Several things at once explains how to use them together. A program using threads runs as native code, in kova run and kova test too.

FunctionDoes
threads.spawn { … }runs the block on a new thread, gives a threads.Task<T>, T being the block’s value
threads.spawn(f)the same with a function without parameters

The block works on its own copies of what it reads and cannot change what is around it.

threads.id -> Int is a number of the running thread, given by the system: two threads running at the same time never have the same one. A number may come back for a thread started after another one ended.

MethodDoes
t.waitwaits for the end of the thread and gives its value; t is used up
WrittenDoes
threads.Channel<T>.newa new channel, without a limit
threads.Channel<T>.limited(n)a new channel holding n values at most (n of 1 or more)
c.send(x)gives x to the channel; waits for room in a limited one; an error on a closed one
c.receivewaits and takes the oldest value: a T?, none once closed and empty
c.receive_within(seconds)the same, for that long at most: none also when nothing came in time
c.closeno more values: the ones inside can still be received
for x in c { … }receives until the channel is closed and empty; each x is the loop’s

A copy of a channel (c.clone, or using it in a spawn block) is the same channel.

threads.select {
x from a => print(x)
s from b => print(s)
after(1.5) => print("too long")
closed => break
}

Waits for the first value of several channels. after(seconds) is optional; closed, which runs once every channel is closed and empty, is required. See Waiting for several channels.

WrittenDoes
threads.Shared<T>.new(value)a value several threads share
s.reada copy of the value
s.change { x: var T => … }lends the value to the block, one thread at a time; gives the block’s value, if any

A copy of a Shared is the same value. A change inside a change of the same value waits for itself: Kova stops the program if no other thread can run.