Threads & Pools

Planned — design subject to change

This feature is designed but not yet implemented. The syntax and behavior described here represent the target specification.

Thread declaration

tape
thread DataLoader {
    @pool(1, 4);  // min 1, max 4 worker threads

    fn load(path: string) -> []u8 or IoError {
        return io.read_file(path);
    }
}

A thread block defines a worker pool. Functions inside run on pool threads, not the calling thread.

Pool sizing

tape
@pool(min, max);
  • min — threads pre-spawned at startup
  • max — maximum concurrent threads (work queues when saturated)

Thread-local state

tape
thread Parser {
    @pool(2, 8);
    var buffer: [4096]u8;  // each pool thread has its own buffer

    fn parse(data: []u8) -> Document {
        // buffer is local to this thread — no sharing
    }
}

var in a thread block is per-thread persistent state — it survives between calls on the same pool thread.

Nested threads

Threads can call other threads:

tape
thread Pipeline {
    @pool(1, 2);

    fn process(input: []u8) -> Result {
        let parsed = Parser.parse(input);     // calls another thread (blocks)
        let validated = Validator.check(parsed);
        return validated;
    }
}

TaskHandle

Async calls return a handle for cancellation:

tape
let task: TaskHandle = DataLoader.load("big.bin") -> on_loaded;

// later:
task.cancel();  // cooperative cancellation

Last modified: