Skip to content

Concurrency & Channels

This content is for v1.0. Switch to the latest version for up-to-date documentation.

RayQuiro 1.0 provides concurrency primitives: spawn for background execution and channel() for message passing between threads.


The spawn keyword executes a function, closure, or callable in a background thread:

spawn fn() {
print("Running in background thread!")
}
fn worker(id) {
print("Worker running:", id)
}
spawn worker(1)
spawn worker(2)

Execution continues immediately on the main thread without blocking.


Channels allow thread-safe bidirectional communication between spawned threads and the main thread.

var ch = channel()
spawn fn() {
var result = 40 + 2
ch.send(result)
}

You can also use the functional helper:

channel.send(ch, "data")

The recv() method blocks until a message is available on the channel:

var msg = ch.recv()
print("Received:", msg)

Or using functional syntax:

var msg = channel.recv(ch)
ch.close()

var queue = channel()
spawn fn() {
for (i = 1; i <= 5; i++) {
queue.send("Task #" + str(i))
}
}
for (i = 1; i <= 5; i++) {
var task = queue.recv()
print("Processing:", task)
}