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.
1. Spawning Threads (spawn)
Section titled “1. Spawning Threads (spawn)”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.
2. Channels (channel())
Section titled “2. Channels (channel())”Channels allow thread-safe bidirectional communication between spawned threads and the main thread.
Creating a Channel
Section titled “Creating a Channel”var ch = channel()Sending Messages (send)
Section titled “Sending Messages (send)”spawn fn() { var result = 40 + 2 ch.send(result)}You can also use the functional helper:
channel.send(ch, "data")Receiving Messages (recv)
Section titled “Receiving Messages (recv)”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)Closing a Channel (close)
Section titled “Closing a Channel (close)”ch.close()3. Producer-Consumer Example
Section titled “3. Producer-Consumer Example”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)}