thread

The thread module provides Langoost’s concurrency primitives. It is VM-based: each spawned function runs in its own isolated VM with a copy of the current globals, so tasks don’t share mutable state unless you opt in via runtime.core. Import it with import thread.

Most primitives are handle-based — new returns an integer id you pass to the other calls, and free releases it. For a tutorial-style introduction with examples, see the concurrency guide.

thread.core

FunctionSignatureDescription
spawnthread.core.spawn(fn)Run fn in a new goroutine
sleepthread.core.sleep(ms: int)Sleep the current task
cpusthread.core.cpus() → intNumber of CPU cores
goroutinesthread.core.goroutines() → intLive goroutine count

thread.pool

FunctionSignatureDescription
newthread.pool.new(workers: int) → intCreate a worker pool
submitthread.pool.submit(id: int, fn) → boolQueue a task
waitthread.pool.wait(id: int)Block until all tasks finish
freethread.pool.free(id: int)Release the pool

thread.async

FunctionSignatureDescription
runthread.async.run(fn) → intRun fn asynchronously; returns a future id
awaitthread.async.await(id: int) → stringBlock for the result
isDonethread.async.isDone(id: int) → boolWhether the future has completed
freethread.async.free(id: int)Release the future

thread.mutex

FunctionSignatureDescription
newthread.mutex.new() → intCreate a mutex
lockthread.mutex.lock(id: int)Acquire (blocks)
unlockthread.mutex.unlock(id: int)Release
tryLockthread.mutex.tryLock(id: int) → boolNon-blocking acquire
freethread.mutex.free(id: int)Release the mutex

thread.rwlock

FunctionSignatureDescription
newthread.rwlock.new() → intCreate a reader/writer lock
rlockthread.rwlock.rlock(id: int)Acquire a read lock (multiple readers)
runlockthread.rwlock.runlock(id: int)Release a read lock
lockthread.rwlock.lock(id: int)Acquire the write lock (exclusive)
unlockthread.rwlock.unlock(id: int)Release the write lock
freethread.rwlock.free(id: int)Release the lock

thread.channel

FunctionSignatureDescription
newthread.channel.new(capacity?: int) → intCreate a channel (unbuffered by default)
sendthread.channel.send(id: int, value: string) → boolSend a value
recvthread.channel.recv(id: int) → stringReceive (blocks; "" when closed)
recvTimeoutthread.channel.recvTimeout(id: int, ms: int) → stringReceive with a timeout
closethread.channel.close(id: int)Close the channel
lenthread.channel.len(id: int) → intBuffered item count
capthread.channel.cap(id: int) → intCapacity

thread.semaphore

FunctionSignatureDescription
newthread.semaphore.new(n: int) → intCreate a semaphore with n permits
acquirethread.semaphore.acquire(id: int)Take a permit (blocks)
releasethread.semaphore.release(id: int)Return a permit
tryAcquirethread.semaphore.tryAcquire(id: int) → boolNon-blocking acquire
freethread.semaphore.free(id: int)Release the semaphore

thread.atomic

FunctionSignatureDescription
newthread.atomic.new(initial?: int) → intCreate an atomic integer
loadthread.atomic.load(id: int) → intRead the value
storethread.atomic.store(id: int, n: int)Write the value
addthread.atomic.add(id: int, n: int) → intAdd and return the new value
swapthread.atomic.swap(id: int, n: int) → intSet and return the old value
casthread.atomic.cas(id: int, old: int, new: int) → boolCompare-and-swap
freethread.atomic.free(id: int)Release the atomic

thread.scheduler

FunctionSignatureDescription
newthread.scheduler.new() → intCreate a scheduler
addthread.scheduler.add(id: int, fn) → boolQueue a function
runthread.scheduler.run(id: int)Run all queued functions in order
sizethread.scheduler.size(id: int) → intNumber of queued functions
freethread.scheduler.free(id: int)Release the scheduler

Example

import thread

let mu = thread.mutex.new()
let counter = thread.atomic.new(0)

fn worker() {
    thread.mutex.lock(mu)
    thread.atomic.add(counter, 1)
    thread.mutex.unlock(mu)
}

let pool = thread.pool.new(4)
let i = 0
while i < 100 {
    thread.pool.submit(pool, worker)
    i += 1
}
thread.pool.wait(pool)

println(thread.atomic.load(counter))   // 100
Standard library · View as Markdown · llms-full.txt