1. What is a green thread
A green thread is a user-space coroutine scheduled by the runtime instead of the operating system kernel. Switching costs are measured in nanoseconds rather than microseconds, so a single process can hold millions of suspended tasks. Meridian green threads cooperate at awaitpoints and can be parked, resumed, or migrated across worker cores without a syscall.
2. Scheduling model
The Meridian scheduler is an M:N work-stealing executor. Each worker owns a local run queue; idle workers steal halves of neighbors' queues. IO is driven by a single shared reactor that wakes parked tasks when their file descriptor becomes ready. Because tasks are tiny, you can treat them as cheap structuring units rather than scarce resources.
3. Minimal example
The snippet below spawns two green threads that communicate over a typed channel. Notice that neither task blocks an OS thread; both yield at the awaitboundary so the scheduler can interleave them on one core.
// Spawning a green thread in Meridian
import { spawn, channel } from '@meridian/runtime';
const ch = channel<number>();
spawn(async () => {
for (let i = 0; i < 5; i++) {
await ch.send(i * i);
}
ch.close();
});
spawn(async () => {
for await (const value of ch) {
console.log('received:', value);
}
});