Nyx by Example

Worker Pool

A worker pool dispatches tasks to a fixed number of threads through a shared channel. Workers pull tasks, process them, and push results to a results channel. This pattern is how http_serve_mt implements its thread pool internally.

Code

// Worker pool — dispatching tasks to N threads via channels
// Pool de workers — despachar tareas a N hilos via canales

var work_ch: Map = Map.new()
var done_ch: Map = Map.new()

fn worker() -> int {
    while true {
        let task: int = channel_recv(work_ch)
        if task < 0 { return 0 }
        // Simulate work: compute square
        let result: int = task * task
        channel_send(done_ch, result)
    }
    return 0
}

fn main() -> int {
    work_ch = channel_new(20)
    done_ch = channel_new(20)

    // Spawn 4 workers
    let num_workers: int = 4
    var handles: Array = []
    var i: int = 0
    while i < num_workers {
        let h: int = thread_spawn(worker)
        handles.push(h)
        i = i + 1
    }

    // Send 8 tasks
    let num_tasks: int = 8
    i = 0
    while i < num_tasks {
        channel_send(work_ch, i + 1)
        i = i + 1
    }

    // Collect results
    i = 0
    while i < num_tasks {
        let result: int = channel_recv(done_ch)
        print("result: " + int_to_string(result))
        i = i + 1
    }

    // Send stop signals
    i = 0
    while i < num_workers {
        channel_send(work_ch, -1)
        i = i + 1
    }

    print("all tasks completed")
    return 0
}

Output

result: 1
result: 4
result: 9
result: 16
result: 25
result: 36
result: 49
result: 64
all tasks completed

Explanation

Two channels coordinate the pool: work_ch distributes tasks and done_ch collects results. Both are declared as Map -- the standard channel type in Nyx.

Four worker threads loop on channel_recv(work_ch), computing the square of each received value and sending results to done_ch. A negative task value serves as a poison pill, causing the worker to exit.

The main thread sends 8 tasks (values 1-8), collects all 8 results, then sends 4 stop signals (one per worker). The order of results may vary between runs because workers execute concurrently, but all 8 squares will appear. This is the same pattern used internally by http_serve_mt to handle HTTP requests across a thread pool.

← Previous Next →

Source: examples/by-example/59-worker-pool.nx