thread
Holding and waiting for the threads that spawn creates.
import thread
In Salam you start a function that should run alongside the rest of the program with spawn. It takes a function with no parameters and no result and gives back a number, the thread's handle. join, or thread.Join, waits for that thread to finish.
The thread package wraps that handle in a Thread so you can keep several of them in a vector and wait for all of them at once. Threads share the program's globals; for shared data use sync or atomic.
Types
Thread
struct Thread| Name | Type | Description |
|---|---|---|
handle | i64 = 0 | The handle spawn returned; zero means invalid. |
import thread
import sync
mut total := 0
mut lock := sync.Mutex {}
func worker():
repeat 1000:
sync.Lock(lock)
total += 1
sync.Unlock(lock)
end
end
func main:
lock = sync.NewMutex()
first := thread.New(spawn(worker))
println thread.Valid(first), thread.Handle(first) != 0
mut group := Vector {} as Vector<thread.Thread>
defer group.free()
repeat 3:
group = thread.Collect(group, spawn(worker))
end
thread.Join(first)
thread.JoinAll(group)
println group.len(), total
empty := thread.New(0)
println thread.Valid(empty), empty.handle
thread.Join(empty)
sync.Destroy(lock)
endFunctions
New
func New(h: i64): Threadspawn handle in a Thread.The example for this item is shown with the ones above: thread-Join.salam
SpawnStack
func SpawnStack(fn: i64, arg: void*, stack: u64): Threadvoid* and returns nothing. If the system cannot give that stack it returns an invalid handle, so check with Valid.import thread
mut total := 0 as i64
func count(n: i64): i64:
if n <= 0:
ret 0
end
ret 1 + count(n - 1)
end
func work(_arg: void*):
total = count(200000)
end
func main:
stack := (64 * 1024 * 1024) as u64
t := thread.SpawnStack(work, null, stack)
println thread.Valid(t)
thread.Join(t)
println total
endJoin
func Join(t: Thread)The example for this item is shown with the ones above: thread-Join.salam
JoinAll
func JoinAll(threads: Vector<Thread>)The example for this item is shown with the ones above: thread-Join.salam
Valid
func Valid(t: Thread): boolThe example for this item is shown with the ones above: thread-Join.salam
Handle
func Handle(t: Thread): i64The example for this item is shown with the ones above: thread-Join.salam
Collect
func Collect(threads: Vector<Thread>, h: i64): Vector<Thread>The example for this item is shown with the ones above: thread-Join.salam