Skip to main content

Module pool

Module pool 

Source
Expand description

A pool of worker threads that lives as long as the backend, from spec/10-cpu.md.

Pool::run hands out tasks 0..n to the calling thread and every worker, and returns when all of them are done. It waits for the tasks, not for the workers: a step of four tasks on a pool of ten returns once the four are done, even when the other workers have not woken up yet. That matters most on a busy machine, where a worker the OS has not scheduled would otherwise hold up every small step. Between jobs a worker spins for a while, since the next op of a plan is usually microseconds away, and then sleeps on a condition variable so an idle server costs nothing. A job is a borrowed closure, so running one allocates nothing.

Tasks are claimed from one word that holds the job’s generation, its task count and the next task, so a worker that wakes up late cannot claim a task of a job that has already finished.

Which thread runs a task depends on timing, but what a task computes does not, and that is what keeps results independent of the thread count.

Structs§

Pool
Worker threads, created once.