NIAOJoin Beta
Ecosystem

parallel

available

Threading & Concurrency

Core Standard Library

Overview

parallel is how you use multiple cores from Niao. Spawn OS threads with a closure, guard shared state with a mutex, or pass work through channels between producers and consumers.

Thread pools accept a batch of jobs and run them on a fixed number of workers. Poll helpers let the VM kick off background work and check completion without blocking the main bytecode loop, important when you're mixing CPU work with I/O.

  • Threads & mutexes
  • Channels
  • Worker pools
  • VM poll helpers

Import

import
import "parallel"

Quick start

quick start
import "parallel"
let pool = parallel.thread_pool(4)
pool.submit(fn() { print("worker running") })
pool.wait_all()

Capabilities

High-level overview of what parallel is for.

Thread & Mutex

thread.spawn runs a closure on a new OS thread. mutex wraps shared data so only one thread mutates it at a time.

Channels

Bounded or unbounded queues for sending messages between threads, classic producer/consumer setup.

Worker pools

Fixed thread count, submit jobs, wait for the batch to finish. Simpler than managing threads by hand.

Poll helpers for VM

Start work in the background and poll from the main loop so the runtime can interleave other tasks.

API reference

25 functions and methods in parallel. Grouped by category from the standard library docs.

Thread - raw OS threads

SignatureDescription
spawn(fn, ...args)Start a thread running a Niao function
join(handle)Wait for result
detach(handle)Detach without joining
is_alive(handle)Whether thread is still running
yield()Hint scheduler
sleep(ms)Sleep current thread

Mutex - shared sendable state

SignatureDescription
new(initial?)Create mutex with optional initial value
lock(handle)Acquire lock
unlock(handle)Release lock
try_lock(handle)Non-blocking lock attempt → `bool`
get(handle)Read value (locked or auto-lock)
set(handle, val)Write value
run(handle, fn)Lock, call `fn()`, unlock

Channel - message passing

SignatureDescription
new(capacity?)Unbounded if omitted; bounded if capacity > 0
send(ch, val)Send value
recv(ch)Blocking receive
try_recv(ch)Non-blocking receive (`nil` if empty)
recv_timeout(ch, ms)Timed receive
close(ch)Close channel
is_closed(ch)Whether channel is closed

Pool - worker thread pool

SignatureDescription
new(workers)Create pool with N worker threads
submit(pool, fn, ...args)Queue work → task id
wait(pool, task_id)Block for result
shutdown(pool)Stop workers
active(pool)Running task count

Notes

  • Database drivers and io use the same background-thread pattern for blocking work.

Related libraries

More from Core Standard Library.

available

json

JSON Parse & Manipulate

Parse, build, and edit JSON. Backed by serde_json in Rust, nested get/set, merge, and pretty-print included.

  • parse & stringify
  • Dot-path access
  • Deep merge
  • Pretty-print
Read more
available

io

File & Path I/O

Read and write files, stream large inputs, and work with paths. Blocking I/O can run on background threads.

  • Whole-file read/write
  • Streaming handles
  • Background I/O
  • Path helpers
Read more
available

time

Wall-Clock & Time Zones

Timestamps, formatting, parsing, and time zones. Built on chrono and chrono-tz, DST handled correctly.

  • IANA time zones
  • strftime formats
  • Datetime values
  • Date math
Read more