Thread
#struct ThreadA thread, running or finished.
| 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++
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)
endtrue true 3 4000 false 0