std/thread

The std/thread module provides high-level primitives for creating and managing concurrent threads of execution.

Overview

Usage

import "std/thread.zc"
import "std/io.zc"

fn worker(id: int) {
    println(f"Hello from worker {id}");
}

fn main() {
    // Spawning with a closure
    let t = Thread::spawn(fn() {
        worker(42);
    }).unwrap();
    
    // Explicitly waiting for completion
    t.join();
}

Struct Definitions

Thread

Represents a handle to a spawned thread.

struct Thread {
    handle: void*;
}

Methods

Thread Lifecycle

MethodSignatureDescription
spawnThread::spawn(func: fn()) -> Result<Thread>Spawns a new thread executing the provided closure or function.
joinjoin(self) -> Result<bool>Blocks the current thread until the spawned thread terminates.
detachdetach(self) -> Result<bool>Detaches the thread, allowing it to run independently. Resources are freed automatically on exit.
cancelcancel(self) -> Result<bool>Sends a cancellation request to the thread.

Utility Functions

FunctionSignatureDescription
sleep_mssleep_ms(ms: int)Suspends execution of the current thread for approximately ms milliseconds.